Home >Backend Development >PHP Tutorial >PHP MySQL: Connect to MySQL database

PHP MySQL: Connect to MySQL database

藏色散人
藏色散人Original
2018-11-02 14:24:464239browse

In this tutorial, we will show you how to connect to a MySQL database server using PDO objects.

Related video tutorial recommendations: mysql tutorial

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

MySQL Data source name or DSN: Specify the address of the MySQL database server. You can use an IP address or server name, for example, 127.0.0.1 or localhost

MySQL database name: Indicates the name of the database to connect to.

Username and Password: Specify the username and password of the MySQL user used to connect to the MySQL database server. This account must have sufficient permissions to access the database specified above.

We will use:

Local MySQL database server, so that the DSN is localhost.

In classicmodels as a sample database.

Root account with blank password, just for demonstration.

Steps to connect to MySQL

First, For convenience, we will create a new PHP file for the database configuration, dbconfig.php Contains all configured parameters:

<?php
    $host = &#39;localhost&#39;;
    $dbname = &#39;classicmodels&#39;;
    $username = &#39;root&#39;;
    $password = &#39;&#39;;

Secondly, we create a new PHP file called phpmysqlconnect.php:

<?php
require_once &#39;dbconfig.php&#39;;
 
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

dbconfig.php uses the require_once function to include the file in the script.

In the try block, we create a new PDO object with three parameters: connection string, username and password. The connection string consists of the $host and $dbname variables in the file dbconfig.php.

If the connection to the MySQL database is successfully established, we will display a success message. If there are any errors or exceptions, PHP will issue a PDOException containing a detailed error message. We call the object's getMesage() method PDOException to get the detailed message to be displayed.

Then we test the script from a web browser.

PHP MySQL: Connect to MySQL database

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

Let's try changing something in the code to make the script display an error message.

If you set the $username variable to empty, you will receive the following error message:

PHP MySQL: Connect to MySQL database

The error message displays:

Access denied for user &#39;&#39;@&#39;localhost&#39; to database &#39;classicmodels&#39;

Because we don't have any blank users in the classicmodels database.

When the script ends, PHP will automatically close 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;

In this tutorial, you have learned how to use the PHP PDO object to connect to MySQL and handle what may happen when connecting to the MySQL database any exception.

The above is the detailed content of PHP MySQL: Connect to MySQL database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn