SOLVED – Fatal Error: Call to undefined function mysql_query()
By Swetha
When you try to load your website in PHP version 7 or above, you will get an error message as “Fatal error: Uncaught Error: Call to undefined function mysql_query() in …“. This happens when you use MySQL Connect or MySQL Extension which works fine in PHP 5.6 or other versions less than 7. But in PHP 7 and above, the MySQL Connect Extension was completely removed and it won’t support.
MySQL Connect
MySQL Connect function is provided for easy access and connection to a MySQL database. You can only access the database by opening the connection. Once the connection is opened, you can interact with the database.
When you use this function on your application, it will create a non-persistent connection to the database management.
The Syntax of MySQL Connect,
mysql_connect(hostname,username,password,databasename);
Parameters:
Hostname: It indicates the server name where the database is located. It is also called servername. It also includes a port number. For example, hostname:3306.
Username: An identification used by a person with access to the MySQL database
Password: The password of the desired user to access the database.
Databasename: The name of the database which the user needs to access.
MySQL_Connect Extension Was Deprecated
The MySQL_Connect is deprecated to provide a secure connection to the Database management.
MySQL_Connect is deprecated in PHP 5.5 and completely removed from PHP 7.0. It will no longer be supported.
When you use that, you will get the error message as
“Fatal error: Uncaught Error: Call to undefined function mysql_query() in …”
Instead, you can use MySQLi_connect or PDO_MySQL to establish a connection to the Database.
Note: i refers to improved which means an improved version of MySQL_Connect.
Solution
1. PHP 5.6 (Temporary)
MySQL_Connect was deprecated on the PHP version above 7.0 but it will work fine on the Version PHP 5.6. So you can use the PHP 5.6 to make it work temporarily but it is not recommended. You can work on the option 2 and option 3 mentioned below which is the more secured and reliable solution.
2. MySQLi_connect
With the MYSQLi_Connect function, you can connect the database more securely than compared with the MySQL_Connect function. If you are only working with the MySQL database, then you can use MySQL_Connect. It follows both procedural and object-oriented.
Syntax:
$servername = “localhost”;
$username = “username”;
$password = “password”;
$vmk = mysqli_connect($servername, $username, $password);
3. PDO_MySQL:
PHP Data Objects(Object-oriented) will support 12 different database management. If you are changing your database type, you can use PDO. For that, you have to change only the connection string and a few queries. It will helpful for you in more ways.
Syntax:
$servername = “localhost”;
$username = “username”;
$password = “password”;
$vmk = new PDO(“mysql:host=$servername;dbname=myDB”, $username, $password);
Note: In PHP Data Objects a valid database is required. If no database name is mentioned, it will throw an exception.