(MySql Php) Call to undefined function mysql_connect
Many programmers using php to connect to mysql face this error while writing/testing their code.
Fatal error: Uncaught Error: Call to undefined function mysql_connect()
Many websites and tutorials contains sample code containing mysql_connect which no longer works. This article provides details on why and what to do to fix this error.
Uncaught Error: Call to undefined function mysql_connect
This error happens because function mysql_connect has been removed since PHP version 7. So if you are using Php 7 or higher, you will see this error.
Their are two main alternative to fix this error.
undefined mysql_connect - use mysqli_connect
The easiest way to fix this is to replace
mysqli_connect. For example:
$mysql = new mysqli("localhost", "user", "password", "database");
Mysqli interace is the recommended solution most of the time. Though their is another solution if you want to support more than just mysql - called POD.
undefined mysql_connect - use PDO
PDO is more generic interface and supports more than 10 different database types. To remove the error, you can redefine your
mysql_connect with following code:
$pdo = new PDO("mysql:host=localhost;dbname=database", 'username', 'password');
Please note that PDO uses object oriented interface for database access, which is different from older