In this article, you will learn to connect to a MySQL database using PHP with the below methods:
Method #1: Connect to MySQL with MySQL Improved
The MySQL Improved extension using the mysqli 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:
<?php mysql_connect('localhost','username','password');mysql_select_db("dbname"); ?>
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:
<?php $result = $mysqli->query("SELECT lastname FROM employees"); ?>
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:
<?php $myPDO = new PDO('mysql:host=localhost;dbname=dbname', 'username', 'password'); ?>
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:
<?php $result = $myPDO->query("SELECT lastname FROM employees"); ?>
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 below steps:
Use the below PHP code for connecting to MySQL and select a database. Change the username, password, and dbname as per yours:
<?php mysql_connect('localhost','username','password'); mysql_select_db("dbname"); ?>
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:
<?php $result = mysql_query('SELECT lastname FROM employees'); ?>
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 MilesWeb database from a home computer or from another web server.
For this, you need to do two things:
1. On the MilesWeb 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 MilesWeb 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:
<?php $mysqli = new mysqli("xxxx.xxx.xxxx", "username", "password", "dbname"); ?>
You will now be able to connect to MySQL with the use of PHP.