How to Connect to MySQL with the use of PHP? Print

  • connect to mysql, connect to mysql with use of php, MySQL, MySQL database, PHP, database
  • 121

In this article, you will learn to connect to a MySQL database using PHP with the below methods:

Table of Contents

Method #1: Connect to MySQL with MySQL Improved

The MySQL Improved extension using the MySQL class replaces the set of legacy MySQL functions.

For connecting to MySQL with the MySQL Improved extension, follow these steps:

Use the below PHP code to connect to MySQL and select a database. Change the username, password, and dbname as per yours:

Once the code connects to MySQL and selects the database, you can run SQL queries and perform other operations. For instance, the below PHP code runs a SQL query that displays the last names from the employees table, and stores the result in the $result variable:

 

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

The MySQL Improved extension works only with MySQL databases. But PDO swipes database access and allows you to create code that can manage different types of databases.

For connecting to MySQL using PDO, follow these steps:

Using the below PHP code connect to MySQL and select a database. Change the username, password, and dbname as per yours:

Once the code connects to MySQL and selects the database, you can run SQL queries and perform other operations. For instance, the below PHP code runs a SQL query that displays the last names from the employees table, and stores the result in the $result variable:

 

Method #3: Connect to MySQL Using Legacy PHP Functions

The original PHP MySQL functions (beginning with mysql_) are disapproved in PHP 5.5, and will finally be removed from PHP. Therefore, only use these functions when extremely necessary for backward compatibility. Use the MySQL Improved extension or PDO instead at the most.

For connecting to MySQL using the legacy PHP MySQL functions, follow the below steps:

Use the below PHP code for connecting to MySQL and select a database. Change the username, password, and dbname as per yours:

Once the code connects to MySQL and selects the database, you are allowed to run SQL queries and perform other operations. For instance, the below PHP code runs a SQL query that displays the last names from the employees table, and stores the result in the $result variable:

 

Method #4: Connect to Remote MySQL Databases with the use of PHP

In the previous examples, it is assumed that the PHP script runs on the same server where the MySQL database is located. But what is the solution if you want to use PHP to connect to a MySQL database from a remote location? For instance, you may want to connect to your Host BD database from a home computer or from another web server.

For this, you need to do two things:

1. On the Host BD server, enable the connecting IP address for remote access.

Note: If you don’t add your IP address to the list of permitted remote access hosts, you will get the Access denied messages while accessing a MySQL database remotely.

2. Go to your PHP code and change the MySQL connection string to use the Host BD server name instead of localhost. Replace “xxxx.xxx.xxxx” with localhost, if you connect using locally and replace it with the server IP, if you connect remotely:

You will now be able to connect to MySQL with the use of PHP.


Was this answer helpful?

« Back