This article describes several methods to connect to a MySQL database using PHP:
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:
<?php $mysqli = new mysqli("localhost", "username", "password", "dbname"); ?>
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"); ?>
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:
<?php $myPDO = new PDO('mysql:host=localhost;dbname=dbname', 'username', 'password'); ?>
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"); ?>
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:
<?php mysql_connect('localhost','username','password'); mysql_select_db("dbname"); ?>
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'); ?>
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 A2 Hosting database from a home computer or from another web server.
To do this, you need to do two things:
<?php $mysqli = new mysqli("a2ss25.a2hosting.com", "username", "password", "dbname"); ?>
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.
We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.