Solutions to the Deprecated Mysql_connect in PHP
-
Connect to MySQL Using the Procedural
Mysqli_connect
- Connect to MySQL Using Object-Oriented Programming
- Connect to MySQL Using PDO
This article will teach you the solutions to the deprecated mysql_connect
in PHP. These solutions include mysqli_connect
(procedural and OOP) and PHP Data Objects (PDO).
Connect to MySQL Using the Procedural Mysqli_connect
mysqli_connect
allows you to use procedural programming to connect to your MySQL database. As a result, you can use the following algorithm:
-
Define the database connection details.
-
Connect with
mysqli_connect
. -
If there is a successful connection, show a success message.
-
If there is a failed connection, show an error message.
This algorithm works fine until an error occurs in step 2. Such an error could be an incorrect database detail.
When PHP encounters these incorrect details, it’ll throw a Fatal Error
exception. Therefore, the code will not get to step 3 or step 4, where you’ll show an error message.
To remediate this, we have to switch off MySQL error reporting and suppress warnings. By doing this, when an error occurs, you’ll prevent the user from seeing sensitive details, and at the same time, you can show a custom error message.
With that said, the following is the modified algorithm:
-
Define the database connection details.
-
Switch off MySQL error reporting.
-
Connect with
mysqli_connect
and suppress warnings. -
If there is a successful connection, show a success message.
-
If there is a failed connection, show an error message.
The following is the implementation of this algorithm:
<?php
// The following are the defaults for a
// new MySQL installation. You should replace
// the $host, $mysql_user, and $mysql_user_password
// with your details.
$host = 'localhost';
$mysql_user = 'root';
$mysql_user_passowrd = '';
// Turn off error reports like "Fatal Errors".
// Such reports can contain too much sensitive
// information that no one should see. Also,
// turning off error reports allows us to handle
// connection error in an if/else statement.
mysqli_report(MYSQLI_REPORT_OFF);
// If there is an error in the connection string,
// PHP will produce a Warning message. For security
// and private reasons, it's best to suppress the
// warnings using the '@' sign
$connect_to_mysql = @mysqli_connect($host, $mysql_user, $mysql_user_passowrd);
if (!$connect_to_mysql) {
echo "<b style='color: red;'>Failed to connect to MySQL.</b>";
} else {
echo "You've made a successful connection to MySQL.";
}
?>
Output for a successful connection:
You've made a successful connection to MySQL.
Output for a failed connection:
<b style='color: red;'>Failed to connect to MySQL.</b>
Connect to MySQL Using Object-Oriented Programming
With OOP and mysqli_connect
, you can create a database connection using new mysqli()
. Like the procedural technique, you must pass the connection details to new mysqli()
.
However, you can have errors in these details, so we’ll use a try...catch
block for error handling. First, we’ll place the connection details in the try
block, and if an error occurs, we’ll catch them in the catch
block.
In the following, the connection to MySQL uses the OOP version of mysqli_connect
. You’ll also notice that we’ve suppressed errors in the connection string.
<?php
// The following are the defaults for a
// new MySQL installation. You should replace
// the $host, $mysql_user, and $mysql_user_password
// with your details.
$host = 'localhost';
$mysql_user = 'root';
$mysql_user_passowrd = '';
try {
// Connect to the database using the OOP style
// of mysqli.
$connection_string = @new mysqli($host, $mysql_user, $mysql_user_passowrd);
echo "You've made a successful connection to MySQL.";
} catch (Exception $e) {
// If an error occurs, access the getMessage()
// method of the $e object. This gives information
// on why the error occurred.
echo "<b style='color: red;'>Failed to connect to MySQL :</b> " . $e->getMessage();
}
?>
Output for a successful connection:
You've made a successful connection to MySQL.
Output for a failed connection:
<b style='color: red;'>Failed to connect to MySQL :</b> Access denied for user ''@'localhost' (using password: NO)
Connect to MySQL Using PDO
You can connect to MySQL using PDO, and you should catch errors using a try... catch
block. The latter works the same way you learned in the OOP section.
In addition, your user won’t see sensitive error messages when an error occurs. Now, the following is the PDO version for connecting to MySQL:
<?php
// The following are the defaults for a
// new MySQL installation. You should replace
// the $host, $mysql_user, and $mysql_user_password
// with your details.
$host = 'localhost';
$mysql_user = 'root';
$mysql_user_passowrd = '';
try {
// Connect to MySQL using PDO and set PDO
// error mode to exception.
$connection_string = @new PDO("mysql:host=$host", $mysql_user, $mysql_user_passowrd);
$connection_string->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "You've made a successful connection to MySQL.";
} catch (PDOException $e) {
// If an error occurs, access the getMessage()
// method of the $e object. This gives information
// on why the error occurred.
echo "<b style='color: red;'>Failed to connect to MySQL:</b> " . $e->getMessage();
}
?>
Output for a successful connection:
You've made a successful connection to MySQL.
Output for a failed connection:
Failed to connect to MySQL: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)
Habdul Hazeez is a technical writer with amazing research skills. He can connect the dots, and make sense of data that are scattered across different media.
LinkedIn