PHP How to connect to MySQL database - PDO

  • Last updated: Nov 3, 2023
  • Views: 802
  • Author: Admin
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

YII2 defaultRoute - How to change default controller in template

YII2 defaultRoute - How to change default controller in template

Pure HTML/CSS search bar

Pure HTML/CSS search bar

HTML/CSS - Expandable Searchbar Animation

HTML/CSS - Expandable Searchbar Animation