. Advertisement .
..3..
. Advertisement .
..4..
Let’s say you are working on your project in the PHP programming language and suddenly receive the Uncaught error: call to undefined function mysql_connect(). What should you do in this case? How can you fix it? This guide will answer all your questions. Continue reading and find out!
What Causes Uncaught error: call to undefined function mysql_connect()?
When attempting to use the mysql_connect()
features of PHP 5 in PHP 7, you might run into the Uncaught error: call to undefined function mysql_connect()
. This error most likely signifies that your server’s PHP does not yet have MySQL support enabled.
It is caused by the entire removal of the mysql_*
functions from PHP 7. They were previously deprecated in PHP 5.5, but not anymore.
The following factors led to the removal of the older MySQL functions:
- Don’t work on the Object-Oriented concept
- Prepared statements and transactions won’t be supported
- Insecure
How To Fix Uncaught error: call to undefined function mysql_connect()
Upgrade to PHP 7
Since you upgraded to PHP 7, mysql_connect
is no longer supported. Check your version with this code:
php -version
Replace it with mysqli_connect as below.
$host = "127.0.0.1";
$username = "root";
$pass = "foobar";
$con = mysqli_connect($host, $username, $pass, "your_database");
If you are updating legacy PHP, you must now replace all of your mysql_*
functions with mysqli_*
.
Utilize MySQLi Or PDO
To prevent the Uncaught error: call to undefined function mysql_connect()
in PHP 7, you should utilize mysqli_connect()
rather than mysql_connect()
.
MySQLi is an API utilized as a connector to link the MySQL database to the PHP application’s backend. You can connect to MySQL database servers using MySQLi functions.
Check out the following example:
$mysql = new mysqli(“localhost”,”root”,“password”,“DB_name”);
You can also use PDO (PHP database objects) to fix this uncaught error. PDO is an abstraction layer for databases. It functions as a backend interface via which modifications to the MySQL database can be made without altering the PHP code.
Here is a sample of utilizing PDO:
$pdo = new PDO(‘mysql:host=localhost;dbname=database_name’, ‘username’, ‘password’);
Keep in mind that a functioning database is necessary for PDO to create connections. In case the database isn’t specified, it raises an exception.
Connect To Mysql With PDO Object
Check out the sample below.
$user = ‘root’; // Mysql
User$password = ''; // Mysql Password
$server = 'localhost'; // Mysql Host
$database = 'my_database'; // Mysql Databse
// PDO Connection string
$pdo = new PDO("mysql:host=$server;dbname=$database", $user, $password);
Connect To Mysql With MySQLi Connection Object
Another way to fix the Uncaught error: call to undefined function mysql_connect() is to connect the database to MySQL with the MySQLi connection object.
Here is an example.
$con = mysqli_connect('localhost', 'username', 'password', 'database');
The Bottom Line
Now you know what to do when receiving the Uncaught error: call to undefined function mysql_connect(). Hopefully, you will find this guide useful in helping solve your problem.
Bonus: We can also assist you if you face other errors in PHP, such as unable to install PHP 7.4 on ubantu 16.04 in apache or the requested PHP extension intl is missing from your system in PHP. Check our tutorials out, and you will soon fix these issues.
Leave a comment