Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PHP is connected to MySQL


May 11, 2021 PHP


Table of contents


PHP connects to MySQL

Before PHP can use MySQL databases, you need to connect them.

PhP 5 and above recommend connecting MySQL in the following ways:

  • MySQLi extension ("i" means improved)
  • PDO (PHP Data Objects)

We used the MySQL extension in the PHP early-morning version. However, the extension has not been recommended since 2012.


Should I use MySQLi or PDO?

If you need a short answer, "You're used to which one."

MySQLi and PDO have their own advantages:

PDO is applied in 12 different databases, and MySQLi is only for MySQL databases.

Therefore, if your project needs to switch between multiple databases, PDO is recommended so that you only need to modify the connection string and some query statements. With MySQLi, if different databases, you need to re-all the code, including queries.

Both are object-oriented, but MySQLi also provides an API interface.

Both support preprocessed statements. Preprocessed statements prevent SQL injection and are important for the security of web projects.


MySQLi and PDO connect MySQL instances

In this and subsequent chapters, we'll demonstrate PHP operation MySQL in three ways:

  • MySQLi (object-oriented)
  • MySQLi (process-oriented)
  • Pdo

MySQLi Installation

Linux and Windows: MySQLi extensions are automatically installed when the php5 mysql package is installed.

For installation details, see: http://php.net/manual/en/mysqli.installation.php


PDO installation

For installation details, see: http://php.net/manual/en/pdo.installation.php


Connect to MySQL

Before we can access the MySQL database, we need to connect to the database server:

Instances (MySQLi - Object Oriented)

<?php
$servername = "localhost";
$username = "username";
$password = "password";

Create a connection
$conn = new mysqli($servername, $username, $password);

Detect the connection
if ($conn->connect_error) {
die("Connection failed: " . $ conn->connect_error);
}
echo "Connected successfully";
?>

PHP is connected to MySQL Note that in the object-oriented $connect above, the error was added in PHP 5.2.9 and 5.3.0. I f you need to be compatible with earlier versions, replace it with the following code:

Detect the connection
if (mysqli_connect_error()) {
die("Database connection failed: " . m ysqli_connect_error());
}

Instances (MySQLi - Process Oriented)

<?php
$servername = "localhost";
$username = "username";
$password = "password";

Create a connection
$conn = mysqli_connect($servername, $username, $password);

Detect the connection
if (!$conn) {
die("Connection failed: " . m ysqli_connect_error());
}
echo "Connected successfully";
?>


Example (PDO)

<?php
$servername = "localhost";
$username = "username";
$password = "password";

try {
$conn = new PDO("mysql:host=$servername; d bname=myDB", $username, $password);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>

PHP is connected to MySQL Note that we have specified a database (myDB) in the PDO instance above. T he PDO needs to set the database name during the connection process. If not specified, an exception is thrown.


Close the connection

The connection closes automatically after the script is executed. You can also use the following code to close the connection:

Instances (MySQLi - Object Oriented)

$conn->close();


Instances (MySQLi - Process Oriented)

mysqli_close($conn);


Example (PDO)

$conn = null;

Now that you know how to connect your MySQL database to PHP, you should learn how PHP created the database!