PHP MySQL: Connecting to MySQL Database

PHP MySQL: Connecting to MySQL Database

 

PHP MySQL: Connecting to MySQL Database




Summary: in this tutorial, we will show you how to connect to the MySQL database server using the PDO object.

Before connecting to a MySQL database, you have to specify the following information:

  • MySQL data source name or DSN :  specifies the address of the MySQL database server. You can use IP address or server name e.g., 127.0.0.1  or  localhost
  • MySQL database name: indicates the name of the database to which you want to connect.
  • Username and password: specify the username and password of the MySQL user that you use to connect to the MySQL database server. The account must have sufficient privileges to access the database specified above.

We will use:

  • The local MySQL database server so the DSN is localhost .
  • The classicmodels as the sample database.
  • The root an account with a blank password, just for the sake of demonstration.

Connecting to MySQL steps

First, to make it convenient, we will create a new PHP file for database configuration named  dbconfig.php that holds all configured parameters:

<?php $host = 'localhost'; $dbname = 'classicmodels'; $username = 'root'; $password = '';

Second, we create a new PHP file named phpmysqlconnect.php :

<?php require_once 'dbconfig.php'; try { $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password); echo "Connected to $dbname at $host successfully."; } catch (PDOException $pe) { die("Could not connect to the database $dbname :" . $pe->getMessage()); }

How the script works.

  • We included the  dbconfig.php file into the script by using the  require_once  function.
  • Inside the try block, we created a new PDO object with three arguments: connection string, username, and password. The connection string is composed of  $host and $dbname  variables in the  dbconfig.php file.
  • If the connection to the MySQL database was established successfully, we displayed a success message. If there were any errors or exceptions, PHP issued a PDOException  that contains the detailed error message. We call the getMesage()  method of the PDOException object to get the detailed message for display.

Third, let’s test the script from the web browser.

It works as expected. We’ve successfully connected to the MySQL server.

Let’s try to change something in the code to make the script display an error message. If you set the  $username variable to blank, you will get the following error message:

The error message shows that:

Access denied for user ''@'localhost' to database 'classicmodels'

because we don’t have any blank users in the classicmodels database.

When the script ends, PHP automatically closes the connection to the MySQL database server. If you want to explicitly close the database connection, you need to set the PDO object to null as follows:

$conn = null;

You can download the scripts of this tutorial via the following download link:

PHP MySQL Connect

In this tutorial, you’ve learned how to connect to MySQL using PHP PDO object and handle any exception that may occur when connecting the MySQL database.

Reactions

Post a Comment

0 Comments

close