Code has been added to clipboard!

Getting to Know PHP Database: Why You Should Use MySQL

Reading time 2 min
Published Aug 8, 2017
Updated Oct 15, 2019

Among the most important types of external systems are the ones made for managing databases. For quite a few years, this sector of the market has been strongly dominated by MySQL.

MySQL is free, open-source, and powerful enough to handle copious amounts of data. It is also flexible and reliable, and setting the connection up takes minutes.

MySQL can also be used as a PHP database. With this combination, the sturdiest and most powerful websites and web applications are made and presented to their users. For those who code in PHP MySQL database provides a possibility to use it cross-platform. That makes their job easier and their approach more flexible.

PHP Database: Main Tips

  • MySQL databases are the standard way of permanently storing data online. It is also ideal for applications of any size.
  • Among those who use PHP, MySQL database system is the most popular, even though the language itself allows using various databases.
  • MySQL is multi-platform and must run on a server.
  • MySQL uses standard SQL syntax.
  • It is free to download and use.
  • Using MySQL PHP coders can store their data in tables consisting of columns and rows.

Database Systems Explained

A PHP MySQL connection is currently the industry standard for storing data for cross-platform use. View the example below to see how it works:

Example
<?php
  $server = 'server';
  $user = 'user';  	
  $pass = 'pass';
  $db = 'db';
  try {    
    $con = new PDO("mysql:host=$server;db=$db", $user, $pass);
    // PDO error mode is set to exception
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // the transaction begins
    $con->beginTransaction();
    // our SQL statements
    $con->exec("INSERT INTO users (name, surname, e_mail) VALUES ('Ben', 'Jefferson', 'ben@example.com')");
    $con->exec("INSERT INTO users (name, surname, e_mail) VALUES ('Johnny', 'Eriksen', 'johnny@example.com')");
    $con->exec("INSERT INTO users (name, surname, e_mail) VALUES ('Mary', 'Julie', 'julie@example.com')");
    // commit the transaction
    $con->commit();
    echo "Created new records.";    
  } catch(PDOException $err) {
    // roll the transaction back if something fails
    $con->rollback();
    echo "Error message: " . $err->getMessage();
  }
  $con = null;  
?>

What Database Query Is

As you establish a PHP MySQL connection, you will start executing queries. A query is essentially a request to receive data from the PHP database you are using via MySQL.

We can combine PHP and SQL syntax to make requests to the database and receive data, which can then be processed in the form of arrays and variables.

Let's see a simple example of a query. You will notice it's using standard SQL syntax:

Example
<?php
  $server = 'server';
  $user = 'user';  	
  $pass = 'pass';
  $db = 'db';
  try {    
    $con = new PDO("mysql:host=$server;db=$db", $user, $pass);
    // PDO error mode is set to exception
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // the transaction begins
    $con->beginTransaction();
    // our SQL statements
    $con->exec("INSERT INTO users (name, surname, e_mail) VALUES ('Ben', 'Jefferson', 'ben@example.com')");
    $con->exec("INSERT INTO users (name, surname, e_mail) VALUES ('Johnny', 'Eriksen', 'johnny@example.com')");
    $con->exec("INSERT INTO users (name, surname, e_mail) VALUES ('Mary', 'Julie', 'julie@example.com')");
    // commit the transaction
    $con->commit();
    echo "Created new records.";    
  } catch(PDOException $err) {
    // roll the transaction back if something fails
    $con->rollback();
    echo "Error message: " . $err->getMessage();
  }
  $con = null;  
?>

Way to Download MySQL

You can download the interface for using MySQL for PHP databases here.

PHP Database: Summary

  • MySQL is free to get and to use, compatible with most platforms and servers, and therefore extremely popular.
  • It is secure and powerful, which makes it perfect for storing significant amounts of data for prolonged periods of time.
  • MySQL requires a server and abides usual SQL syntax rules.
  • The data is contained in tables made of columns and rows.
Tutorial
Introduction
Installation
Syntax
Variable
Superglobals
Data Types
String
Array
Multidimensional Array
Sort Array
Constant
Operators
Cookies
Sessions
DateTime
Error Handling
Exception Handling
File
Write and Create File
File Open, Read and Close
File Upload
Filtering
Redirecting
Advanced Filters
Forms
Form Required Field
Validate Email/URL
Form Validation
Form Action
Function
Prepared Statements
JSON
Calendar
ZIP File
FTP
HTTP Response
DateTime Functions
Error Functions
File Function
Filter
Math Functions
Mail Function
Miscellaneous Functions
Date Format
String Functions
Array Functions
Directory Functions
MySQL Database
MySQL Connection
MySQL Create Database
MySQL Create Table
MySQL Delete Data
MySQL Insert Data
MySQL Get Last Record ID
MySQL Insert Multiple Records
MySQL Select Data
MySQL Limit Data
MySQL Update Data
MySQLi Functions
AJAX and MySQL
AJAX Search
AJAX Poll
RSS Reader
Read XML File in PHP
XML Parser
SimpleXML Parser
SimpleXML: Node and Attribute
Expat XML Parser
DOMDocument
Libxml Functions
SimpleXML Functions
XML Parsing Functions
PHP isset
PHP echo and print
PHP if else and elseif
PHP switch case
PHP include File
PHP while Loop
PHP for and foreach
PHP mail()
PHP explode()
PHP substr()
PHP str_replace()
PHP array_push
PHP count()