This article describes several methods to connect to a MySQL database using PHP:

  • MySQL Improved (mysqli) PHP extension
  • PDO (PHP Data Objects)
  • Legacy MySQL (mysql_) functions
  • Connect to remote MySQL database using PHP
The MySQL databases and users must already exist before you can use these methods. 

Method 1: Connect to MySQL using MySQL Improved

The MySQL Improved extension uses the mysqli class, which replaces the set of legacy MySQL functions.

To connect to MySQL using the MySQL Improved extension, follow these steps:

  1. Use the following PHP code to connect to MySQL and select a database. Replace username with your username, password with your password, and dbname with the database name:
    <?php
        $mysqli = new mysqli("localhost", "username", "password", "dbname");
    ?>
    
  2. You can conduct SQL queries and other operations after the code connects to MySQL and selects the database. For instance, the PHP code below executes a SQL query that extracts the last names from the employees table and stores the results in the $result variable:

    <?php
        $result = $mysqli->query("SELECT lastname FROM employees");
    ?>
    

Method 2: Connect to MySQL using PHP Data Objects (PDO)

MySQL Improved is only compatible with MySQL databases. PDO, on the other hand, isolates database access and allows you to write code that can work with a variety of databases.

To connect to MySQL using PDO, follow these steps:

  1. Use the following PHP code to connect to MySQL and select a database. Replace username with your username, password with your password, and dbname with the database name:
    <?php
        $myPDO = new PDO('mysql:host=localhost;dbname=dbname', 'username', 'password');
    ?>
    
  2. You can conduct SQL queries and other operations after the code connects to MySQL and selects the database. For instance, the PHP code below executes a SQL query that extracts the last names from the employees table and stores the results in the $result variable:

    <?php
        $result = $myPDO->query("SELECT lastname FROM employees");
    ?>
    

Method 3: Connect to MySQL using legacy PHP functions

The original PHP MySQL functions (whose names begin with mysql_) are deprecated in PHP 5.5 and will be removed from PHP in the future. As a result, you should only utilize these functions for backward compatibility when absolutely essential. Instead, utilize the MySQL Improved extension or PDO if possible.

To connect to MySQL using the legacy PHP MySQL functions, follow these steps:

  1. Use the following PHP code to connect to MySQL and select a database. Replace username with your username, password with your password, and dbname with the database name:
    <?php
        mysql_connect('localhost','username','password');
        mysql_select_db("dbname");
    ?>
    
  2. You can conduct SQL queries and other operations after the code connects to MySQL and selects the database. For instance, the PHP code below executes a SQL query that extracts the last names from the employees table and stores the results in the $result variable:

    <?php
        $result = mysql_query('SELECT lastname FROM employees');
    ?>
    

Method 4:Connecting to remote MySQL databases using PHP

The previous examples all assume that the PHP script runs on the same server where the MySQL database is located. But what if you want to use PHP to connect to a MySQL database from a remote location? For example, you may want to connect to your Hostginger Hosting database from a home computer or from another web server.

To do this, you need to do two things:

  • On the Hostginger Hosting server, enable the connecting IP address for remote access.
  • In your PHP code, change the MySQL connection string to use the Hostginger Hosting server name instead of localhost.  For example, the following PHP code uses mysqli to connect to the Hostginger Hosting server wp1.hg-servers.com:
    <?php
        $mysqli = new mysqli("wp1.hg-servers.com", "username", "password", "dbname");
    ?>
    
Was this answer helpful? 0 Users Found This Useful (0 Votes) mySQL, mySQL connection