Cyber Week

Save big!

All courses under $5 - for a limited time!

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', '[email protected]')");
    $con->exec("INSERT INTO users (name, surname, e_mail) VALUES ('Johnny', 'Eriksen', '[email protected]')");
    $con->exec("INSERT INTO users (name, surname, e_mail) VALUES ('Mary', 'Julie', '[email protected]')");
    // 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', '[email protected]')");
    $con->exec("INSERT INTO users (name, surname, e_mail) VALUES ('Johnny', 'Eriksen', '[email protected]')");
    $con->exec("INSERT INTO users (name, surname, e_mail) VALUES ('Mary', 'Julie', '[email protected]')");
    // 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.