Troubleshooting Guide: Fixing the Uncaught Error - Call to Undefined Function mysql_query()

The mysql_query() function has been deprecated as of PHP 5.5.0 and removed entirely in PHP 7.0.0. If you are using a newer version of PHP and encounter the "Uncaught Error: Call to undefined function mysql_query()" error, you will need to update your code to use the newer mysqli or PDO extension instead.

In this guide, we will provide you with a step-by-step solution on how to update your code and fix the "Call to undefined function mysql_query()" error.

Table of Contents

  1. Understanding the error
  2. Updating your code to use mysqli
  3. Updating your code to use PDO
  4. FAQ

Understanding the error

The "Call to undefined function mysql_query()" error occurs when your PHP code attempts to use the mysql_query function, which is no longer available in PHP versions 7.0.0 and above. The mysql_* functions have been deprecated due to various reasons, such as lack of support for prepared statements, which can lead to SQL injection vulnerabilities.

To fix this error, you need to update your code to use either the mysqli or PDO extension. Both extensions provide support for prepared statements, making your code more secure.

Updating your code to use mysqli

Follow these steps to update your code to use the mysqli extension:

  1. Replace the mysql_connect() function with mysqli_connect():
// Old code
$link = mysql_connect('localhost', 'username', 'password');

// New code
$link = mysqli_connect('localhost', 'username', 'password');
  1. Replace the mysql_select_db() function with mysqli_select_db():
// Old code
mysql_select_db('database_name', $link);

// New code
mysqli_select_db($link, 'database_name');
  1. Replace the mysql_query() function with mysqli_query():
// Old code
$result = mysql_query('SELECT * FROM table_name');

// New code
$result = mysqli_query($link, 'SELECT * FROM table_name');
  1. Update other mysql_* functions, such as mysql_fetch_assoc(), mysql_num_rows(), etc. with their corresponding mysqli_* functions:
// Old code
$row = mysql_fetch_assoc($result);

// New code
$row = mysqli_fetch_assoc($result);

Updating your code to use PDO

Follow these steps to update your code to use the PDO extension:

  1. Replace the mysql_connect() and mysql_select_db() functions with a new PDO instance:
// Old code
$link = mysql_connect('localhost', 'username', 'password');
mysql_select_db('database_name', $link);

// New code
$pdo = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');
  1. Replace the mysql_query() function with PDO::query():
// Old code
$result = mysql_query('SELECT * FROM table_name');

// New code
$result = $pdo->query('SELECT * FROM table_name');
  1. Update other mysql_* functions, such as mysql_fetch_assoc(), mysql_num_rows(), etc. with their corresponding PDO methods:
// Old code
$row = mysql_fetch_assoc($result);

// New code
$row = $result->fetch(PDO::FETCH_ASSOC);

FAQ

Why were the mysql_* functions removed in PHP 7.0.0?

The mysql_* functions were removed because they lacked support for prepared statements, which can lead to SQL injection vulnerabilities. The newer mysqli and PDO extensions provide support for prepared statements and other improved features, making them a more secure and efficient choice.

Can I still use the mysql_* functions in PHP 5.6?

While the mysql_* functions are still available in PHP 5.6, they are deprecated and should not be used in new projects. It's recommended to update your code to use either the mysqli or PDO extension.

What is the difference between mysqli and PDO?

Both mysqli and PDO are PHP extensions that provide access to MySQL databases. The main difference between the two is that mysqli is specific to MySQL databases, while PDO is a database abstraction layer that supports multiple databases, including MySQL, PostgreSQL, SQLite, and others.

How can I check if my server is running PHP 7.0.0 or higher?

You can check your PHP version using the phpinfo() function. Create a file called phpinfo.php with the following content:

<?php
    phpinfo();
?>

Upload the file to your server and access it via your web browser. The PHP version will be displayed at the top of the page.

How do I enable error reporting in PHP?

To enable error reporting, add the following lines at the beginning of your PHP script:

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
?>

These lines will enable error reporting and display any errors encountered while executing your script.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Lxadm.com.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.