PHP - How to Connect to MySQL  screenshot

PHP - How to Connect to MySQL

If you already know the basics of writing PHP scripts, you will of course be aware that PHP consists of built-in functions that can be used to connect to MySQL databases for storage and retrieval activities. In this tutorial we’ll learn how to establish a connection to a MySQL database from within a PHP script.

Before you can access data in a database, you must know how to establish a connection to your MySQL server and authenticate yourself. The functions mysql_connect() and mysqli_connect() can be used to achieve this. We’ll discuss both methods of connectin g to a MySQL database; however, it should be noted that mysql_connect() has been depreciated as of PHP 5.5.0, whereas you will need a server that supports PHP 5 or above to use mysqli_connect().

Establish connection using mysql_connect()

i. Use the mysql_connect() function to establish connection to a MySQL database and pass the return of that function to a variable.

<?php
/**
* Class and Function List:
* Function list:
* Classes list:
*/
$db_connect = mysql_connect(localhost’, ‘mysql_user’, ‘mysql_password);
?>

localhost is the hostname for our MySQL server. You can define a port or substitute it with an IP address—for example “192.168.0.1:3307”.

mysql_user is the username with access privileges for the MySQL database you are going to access in the next step. Depending on the Web host configuration, this can be the same for all databases present on a MySQL server, or different for each database.

mysql_password is the password for the username being used to access the MySQL server; in this case it’s mysql_user.

Once the code is executed, PHP will attempt to establish connection with MySQL server.

ii. Now we are ready to access a database present on the MySQL server, but before that we need to ensure that we have successfully connected to the MySQL server. This can be achieved as follows.

<?php
/**
* Class and Function List:
* Function list:
* Classes list:
*/
$db_connect = mysql_connect(localhost’, ‘mysql_user’, ‘mysql_password);
if (!db_connect) {
    die(Connectionunsuccessful:
        ‘ . mysql_error());
    }
    echoConnectionsuccessful’;
?>

If mysql_connect() function returns FALSE, code execution will stop and a relevant error message will be displayed. Otherwise, it’ll pass a MySQL link identifier which will be used to access MySQL database in the next step.

iii. We will use mysql_select_db() function to select the active database for the connection established in the previous steps.

<?php
/**
* Class and Function List:
* Function list:
* Classes list:
*/
$db_connect = mysql_connect(localhost’, ‘mysql_user’, ‘mysql_password);
if (!db_connect) {
    die(Connectionunsuccessful:
        ‘ . mysql_error());
    }
    echoConnectionsuccessful’;
    $db_select = mysql_select_db(db_name’, $db_connect);
    if (!db_select) {
        die(Unabletoaccessdatabase:
            ‘ . mysql_error());
        }
        echoDatabaseselected’;
?>

If mysql_select_db() functions returns TRUE then every subsequent call for executing database queries will be made on the active database, in this case db_name.

Establish connection using mysqli_connect()

Use the mysqli_connect() function to establish connection to a MySQL database and pass the return of that function to a variable.

<?php
/**
* Class and Function List:
* Function list:
* Classes list:
*/
$db_connect = mysqli_connect("localhost", "mysql_user", "mysql_password", "mysql_db");
if (!db_connect) {
    die(Connectionunsuccessful:
        ‘ . mysqli_connect_errno());
    }
    echoConnectionsuccessful’;
?>

localhost is the hostname for our MySQL server. You can define a port or substitute it with an IP address; for example “192.168.0.1:3307”.

mysql_user is the username with access privileges for the MySQL database you are going to access in the next step. Depending on the Web host configuration, this can be same for all databases present on a MySQL server, or different for each database.

mysql_password is the password for the username being used to access the MySQL server; in this case its mysql_user.

mysql_db is the database maintained on our MySQL server

Once the code is executed, PHP will attempt to establish a connection with MySQL server. If the connection is unsuccessful then the relevant error code will be displayed.