English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP MySQL Connection

In this tutorial, you will learn how to connect to the MySQL server using PHP.

Methods to connect to MySQL through PHP

To store or access data in the MySQL database, you first need to connect to the MySQL database server. PHP provides two different ways to connect to the MySQL server:MySQLi(Improved MySQL) andPDO(PHP Data Objects) extension.

Although the PDO extension name has better portability and supports more than twelve different databases, as the name implies, the MySQLi extension name only supports MySQL databases. However, the MySQLi extension provides a convenient method to connect to a MySQL database server and execute queries in it. Both PDO and MySQLi provide an object-oriented API, but MySQLi also provides a procedural API, which is relatively easy to understand for beginners.

Tip:Compared to the PDO extension, PHP's MySQLi extension provides both speed and functional advantages, so it may be a better choice for projects specific to MySQL.

Connect to the MySQL database server

In PHP, you can easily perform this operation using the mysqli_connect() function. All communication between PHP and the MySQL database server is done through this connection. The following are the three basic syntaxes for connecting to MySQL using MySQLi and PDO extensions:

Syntax: MySQLi, procedural method

$link = mysqli_connect("hostname", "username", "password", "database");

Syntax: MySQLi, object-oriented method

$mysqli = new mysqli("hostname", "username", "password", "database");

Syntax: PHP Data Objects (PDO) method

$pdo = new PDO("mysql:host=hostname;dbname=database", "username", "password");

In the above syntax, the hostname parameter specifies the hostname (e.g., localhost) or the IP address of the MySQL server, while the username and password parameters specify the credentials to access the MySQL server, and the database parameter (if provided) specifies the default MySQL database to be used when executing queries.

The following example demonstrates how to use MySQLi (programandobject-orientedmethods) and PDO extension to connect to the MySQL database server.

Online example

<?php
/* Attempt to connect to the MySQL server. Assume you are running MySQL.
Server with default settings (user "root" without a password) */
$link = mysqli_connect("localhost", "root", ");
 
// Check the connection
if($link === false){
    die("Error: Unable to connect." . mysqli_connect_error());
}
 
//Print the host information
echo "Connection successful. Host information: " . mysqli_get_host_info($link);
?>
<?php
/* Attempt to connect to the MySQL server. Assume you are running MySQL.
Server with default settings (user "root" without a password) */
$mysqli = new mysqli("localhost", "root", "", "demo");
 
// Check the connection
if($mysqli === false){
    die("Error: Unable to connect." . $mysqli->connect_error);
}
 
//Print the host information
echo "Connection successful. Host information: " . $mysqli->host_info;
?>
<?php
/*Attempt to connect to the MySQL server. Assume you are running MySQL.
Server with default settings (user "root" without a password) */
try{
    $pdo = new PDO("mysql:host=localhost", "root", ");
    
    //Set PDO error mode to exception
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    //Print the host information
    echo "Connection successful. Host information: " . $pdo->getAttribute(constant("PDO::ATTR_CONNECTION_STATUS"));
} catch(PDOException $e){
    die("Error: Unable to connect.\n" . $e->getMessage());
}
?>

Note: The default username for MySQL database servers is root, with no password. However, to prevent the database from being hacked and unauthorized access, you should set a password for the MySQL account.

Tip:Setting the PDO::ATTR_ERRMODE attribute to PDO::ERRMODE_EXCEPTION will tell PDO to throw an exception when a database error occurs.

Close the connection to the MySQL database server

After the script execution is complete, the connection with the MySQL database server will be automatically closed. However, if you want to close it early, you just need to call the PHP mysqli_close() function.

Online example

<?php
/*Attempt to connect to the MySQL server. Assume you are running MySQL.
Server with default settings (user "root" without a password)*/
$link = mysqli_connect("localhost", "root", ");
 
// Check the connection
if($link === false){
    die("Error: Unable to connect." . mysqli_connect_error());
}
 
// Print the host information
echo "Connection successful. Host information: " . mysqli_get_host_info($link);
 
// Close the connection
mysqli_close($link);
?>
<?php
/*Attempt to connect to the MySQL server. Assume you are running MySQL.
Server with default settings (user "root" without a password) */
$mysqli = new mysqli("localhost", "root", "", "demo");
 
// Check the connection
if($mysqli === false){
    die("Error: Unable to connect." . $mysqli->connect_error);
}
 
// Print the host information
echo "Connection successful. Host information: " . $mysqli->host_info;
 
// Close the connection
$mysqli->close();
?>
<?php
/* Attempt to connect to the MySQL server. Assume you are running MySQL.
Server with default settings (user "root" without a password) */
try{
    $pdo = new PDO("mysql:host=localhost;dbname=demo", "root", "");
    
    //Set PDO error mode to exception
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    //Print the host information
    echo "Connection successful. Host information: " . $pdo->getAttribute(constant("PDO::ATTR_CONNECTION_STATUS"));
} catch(PDOException $e){
    die("Error: Unable to connect." . $e->getMessage());
}
 
//Close the connection
unset($pdo);
?>