> ## Documentation Index
> Fetch the complete documentation index at: https://kb.hosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Connecting to MySQL using PHP

> Master PHP-MySQL connections with this guide featuring methods, detailed instructions, code snippets, and related articles.

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.

* Connecting to remote MySQL databases using PHP.

<Tip>
  The MySQL databases and users must already exist before you can use these methods. For information about how to manage MySQL databases using cPanel, please see [this article](/docs/managing-mysql-databases).
</Tip>

## 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. After the code connects to MySQL and selects the database, you can run SQL queries and perform other operations. For example, the following PHP code runs a SQL query that extracts the last names from the *employees* table, and stores the result in the *\$result* variable:

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

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

The MySQL Improved extension can only be used with MySQL databases. PDO, on the other hand, abstracts database access and enables you to create code that can handle different types 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. After the code connects to MySQL and selects the database, you can run SQL queries and perform other operations. For example, the following PHP code runs a SQL query that extracts the last names from the *employees* table, and stores the result 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 eventually be removed from PHP. Therefore, you should only use these functions when absolutely necessary for backward compatibility. If possible, use the MySQL Improved extension or PDO instead.

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. After the code connects to MySQL and selects the database, you can run SQL queries and perform other operations. For example, the following PHP code runs a SQL query that extracts the last names from the *employees* table, and stores the result in the *\$result* variable:

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

## 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 hosting.com database from a home computer or from another web server.

To do this, you need to do two things:

* On the hosting.com server, enable the connecting IP address for remote access. For information about how to do this, please see [this article](/docs/remote-mysql-access).
  > 📘 Note
  >
  > If you do not add your IP address to the list of permitted remote access hosts, you receive **Access denied** messages when you try to access a MySQL database remotely.

* In your PHP code, change the MySQL connection string to use the hosting.com server name instead of *localhost*. For example, the following PHP code uses *mysqli* to connect to the hosting.com server mi3-ss25.a2hosting.com:

```
<?php
    $mysqli = new mysqli("mi3-ss25.a2hosting.com", "username", "password", "dbname");
?>
```

<Note>
  For information about how to determine your account's server name, please see [this article](/docs/determining-your-accounts-server-name).
</Note>

## More information

* For more information about the MySQL Improved extension in PHP, please visit [http://www.php.net/manual/en/book.mysqli.php](http://www.php.net/manual/en/book.mysqli.php).

* For more information about PDO, please visit [http://www.php.net/manual/en/book.pdo.php](http://www.php.net/manual/en/book.pdo.php).

* For more information about the legacy MySQL functions in PHP, please visit [http://www.php.net/manual/en/book.mysql.php](http://www.php.net/manual/en/book.mysql.php).

## Related articles

* [Connecting to MySQL from the command line](/docs/connect-to-mysql-from-the-command-line)

* [Connecting to MySQL using Python](/docs/connecting-to-mysql-using-python)

* [Connecting to MySQL using Perl](/docs/connect-to-mysql-using-perl)

* [Connecting to MySQL using Microsoft.NET](/docs/connecting-to-mysql-using-microsoft-net)

* [Managing MySQL databases, users, and tables from the command line](/docs/managing-mysql-databases-and-users-from-the-command-line)
