Knowledgebase Home / How to Connect to MySQL Using PHP
// View Comments //

How to Connect to MySQL Using PHP

This article describes several PHP methods for connecting to a MySQL database. In this section, we will look at four different ways to connect to MySQL using PHP code.

  • MySQL Improved (mysqli) PHP extension
  • PDO (PHP Data Objects)
  • Legacy MySQL (mysql_) functions
  • Connect to remote MySQL database using PHP

Connect to MySQL using MySQL Improved

First, you will have to log in to your cPanel account. Then create a new MySQL database. If you haven’t already done so, create a new user as well with the necessary privileges for that database. 

Now create a new PHP file in your cPanel account. In the PHP file, use the following code to establish a connection to the MySQL database.

<?php
$servername = "localhost"; //replace localhost with your host name if it's different
$username = "yourusername"; //replace yourusername with your MySQL username
$password = "yourpassword"; //replace yourpassword with your MySQL password
$dbname = "yourdbname"; //replace yourdbname with your MySQL database name

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

Then Save the file and navigate to it in your web browser(Load the PHP file). If the connection is successful, you should see the “Connected successfully” message. If not, check your MySQL database credentials and try again.

Connect to MySQL using PHP Data Objects (PDO)

Please take the same action, If you haven’t already, make a fresh MySQL database and give a fresh user the proper access rights to that database. The PHP file should contain the following code to create a PDO connection to the MySQL database. Please save the file and open it in your web browser after doing so. You should see the message “Connected successfully” if the connection is successful. If not, verify your credentials for the MySQL database and try again.

<?php
$servername = "localhost"; //replace localhost with your host name if it's different
$username = "yourusername"; //replace yourusername with your MySQL username
$password = "yourpassword"; //replace yourpassword with your MySQL password
$dbname = "yourdbname"; //replace yourdbname with your MySQL database name

// Create connection
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>

Note: PDO provides a higher level of abstraction than MySQLi and supports multiple database platforms. Additionally, PDO allows you to use prepared statements, which can help prevent SQL injection attacks.

Connect to MySQL using legacy PHP functions

The identical procedures, including making a new MySQL database, must be followed. Please establish a new user with the required access rights for that database if you haven’t already. Your cPanel account should have a new PHP file created. To connect to the MySQL database using mysql_connect(), enter the following code in the PHP file. Afterward, kindly save the file and open it in your web browser. You ought to see the message “Connected successfully” if the connection is successful. If not, verify your credentials for the MySQL database and try again.

<?php
$servername = "localhost"; //replace localhost with your host name if it's different
$username = "yourusername"; //replace yourusername with your MySQL username
$password = "yourpassword"; //replace yourpassword with your MySQL password
$dbname = "yourdbname"; //replace yourdbname with your MySQL database name

// Create connection
$conn = mysql_connect($servername, $username, $password);

// Check connection
if (!$conn) {
die("Connection failed: " . mysql_error());
}
echo "Connected successfully";
?>

Note: The mysql_connect() function is deprecated as of PHP 5.5.0 and has been removed in PHP 7.0.0. It is recommended to switch to MySQLi or PDO for database connections.

Connect to remote MySQL database using PHP

For example, you may want to connect to your VeeroTech Hosting database from a home computer or from another web server. On the VeeroeTech Hosting server, enable the connecting IP address for remote access. In your PHP code, change the MySQL connection string to use the VeeroTech Hosting server name instead of localhost.

<?php

$mysqli = new mysqli("remote_server_address_or_ip", "dbusername", "password", "dbname");

?>

Replace dbusername with your username, password with your database username password, and dbname with the database name

Conclusion

Congratulations! You have learned how to connect to MySQL using PHP.


If you have any web hosting questions please feel free to reach out to us. We're happy to help.
Shared Hosting | Reseller Hosting | Managed WordPress Hosting | Fully Managed VPS Hosting

Our Guiding Principles

  • Provide consistent, stable, and reliable web hosting services.
  • Ensure rapid ticket response and quick resolutions to issues.
  • Never saturate or over-provision servers to ensure stability and speed for our customers.
  • Use only high-quality enterprise-class hardware to ensure minimal downtime from hardware failures.
  • Provide clear pricing with no hidden fees or gotchas.
Subscribe to comment notifications
Notify of
guest
0 Comments
Inline Feedbacks
View all comments