PHP How to connect to MySQL database - PDO

PHP How to connect to MySQL database - PDO

Colleagues hello to all.

In today's article, we'll talk today  how to connect to a MySQL database using the PHP programming language and the PDO extension.

The PHP programming language supports working with different databases, including MySQL. In order to successfully work with the MySQL database, you do not need anything to install and configure, all the necessary functions already available with the standard PHP distribution. Before you can do anything with a database, you must first connect to it.

To connect to the database, we will use PDO technology, which means (PHP Data Objects). PDO extensions are great for many tasks because it is not tied to a specific database management system. PDO has support databases such as: MySQL, PostgreSQL, SQLite, Oracle, Microsoft SQL Server and others.

An example of connecting to a MySQL database.

$host = "192.168.4.11";
$username = "inzhener";
$password = "Qwerty123!";
$db_name = "inzhener";

$result_connect = connect_pdo($host, $username, $password, $db_name);

function connect_pdo($host, $username, $password, $db_name){
    try {
        $conn = new PDO("mysql:host=$host;dbname=$db_name", $username, $password);
        return $conn;
    } catch (PDOException $e) {
        return 'code_error - '.$e->getCode().' || '.'message_error - '.$e->getMessage();
    }
}

php connect to mysql pdo

In this example, we've made a normal function called connect_pdo, and it will take four required parameters.

  • $host - IP address of the server where MySQL DBMS is installed.
  • $username - Database username.
  • $password - Database user password.
  • $db_name - The name of the database we created in MySQL.

All of these parameters are mandatory. If you do not specify at least one of these parameters, then PDO will return an error.

In the  connect_pdo function itself, we will make connections to the database. In the try catche construct, we need to create a PDO object and pass four parameters to the constructor, and then return this object for further operations with the database. Using the  try catche construction allows us to catch errors in the database if any.

For example, if we accidentally pass an invalid login to connect to the database, a PDOException will be thrown.

php connect to mysql pdo

If you did everything right and passed all the parameters you need, then as a result you should get an empty PDO object.

object(PDO)#1 (0) { }

php connect to mysql pdo

Everything! After getting the PDO object, we can now query the database.


 

Thank you all, I hope that my article helped you in some way.

 

SIMILAR ARTICLES

jQuery - Image gallery with zoom on hover
jQuery - Image gallery with zoom on hover
WEB
Read more
Zoom blowup.js - Zoom blowup plugin with magnifying glass
Zoom blowup.js - Zoom blowup plugin with magnifying glass
WEB
Read more
jQuery Zoom - Image magnifier glass effect
jQuery Zoom - Image magnifier glass effect
WEB
Read more