mysql_connect() and mysql_query()

Error Fix: Call to Undefined Function mysql_connect()

When you try to execute any PHP code through the application or any process that involves a PHP file through WordPress like the upgrade and database connection, you will face the fatal error when using the “Call to Undefined Function mysql_connect() or Call to Undefined Function mysql_query()” functions in PHP older versions which might be difficult to resolve or understand the PHP functions unless you are a developer. Let’s delve deep into the topic.

mysql_connect()

 

MySQL Extension Deprecation:

The MySQL extension is deprecated in PHP 5.5.0 and is fully removed in the PHP 7 release. Since the deprecation of MySQL or mysql_connect() function in PHP, developers are advised to use Mysqli() or PDO() to connect to databases. Errors related to “Call to Undefined Function mysql_connect()” typically indicate that the script is outdated.

PHP Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /home/user/public_html/script.php

If facing an error while working on the application,

PHP Fatal error: Uncaught Error: Call to undefined function mysql_query() in /Applications/path/script.php

Mysql extension will need to be migrated to either Mysqli() or PDO() it runs on a server/website running PHP7 and its newer versions.

 

Reasons to Migrate from older PHP version and extension:

1. Not provide the Object-Oriented concept.

2. Not allow/support transactions and prepared statements.

3. Insecure connection to database.

4. WordPress or Plugin version not compatible.

5. Mysql Functions [mysql_connect()] not support latest MySQL features.

6. Functions/code used were a mix of mysql and mysqli (Using the Database Connections object with the MySQL function, but querying with the mysqli functions or vice versa).

7. Properly configured but not enabled mysqlnd extension.

8.  MySQL function code is wide open to SQL injection attacks.

 

Methods to Fix Call to Undefined Function mysql_connect():

1. Use MySQL improved extension (MySQLi) or PHP Data Objects (PDO) with PHP version above 7

2. Replace PHP files and update the website code with old version.

Eg, Replace all instances of mysql_query() with mysqli_query().

3. PHP version below 7 to support mysql_connect() function (Not recommended and insecure)

4. Update CMS to version compatible with the mysqli extension.

5. Avoid mixing deprecated mysql with mysqli extension as well code in your php file.

6. Install mysqlnd extension on your configured php version.

 

Additional Resource:

MySQL Functions (PDO_MYSQL)

PHP Connect to MySQL

MySQL API

PhpMyAdmin

 

MySQL database Connection Code:

1. PHP Mysqli:

Mysqli() extension is an improved version of the older PHP Mysql driver and it’s used in the PHP scripting languages and intersects with the MySQL database. Procedural programmers prefer Mysqli(). But it will support both Procedural and object-oriented interfaces. Mysqli is a relational database and it provides various advantages. Also it provides a more secure and feature-rich way to interact with MySQL databases.

NOTE:

1. When you use a database in a program, it’s important to properly close the connection to the database when you’re done using it. So it doesn’t run unnecessarily.
2. Now, persistent connections are a bit different. Instead of opening and closing the connection every time you need to use the database, you keep it open for multiple users.
3. Fixed connections are involved in automatic cleanup, which means the system takes care of closing the connection for you when it’s not needed, which helps prevent unnecessary usage of resources and keeps everything running smoothly.

 

Two Primary Functions in PHP Mysql:

1. PHP mysql_connect() function – connect to a MySQL database server

2. PHP mysqli_query function – execute SQL queries using the database connected

  • Insert
  • Select
  • Update
  • Delete

 

Key differences between the two extensions with below example:

Mysql()

Mysql_connect (“localhost”, “username”, “password”);

Mysqli()

Mysqli() – requires a connection identifier.  We can store connection to the PHP variable.

$conn = Mysql_connect (“localhost”, “username”, “password”);

Some of the Mysqli() functions are listed below,

mysqli_get_host_info()
mysqli_get_proto_info()
mysqli_autocommit()
mysqli_change_user()
mysqli_commit()
mysqli_errno()
mysqli_error()
mysqli_get_connection_stats()
mysqli_warning_count()

 

2. PHP PDO()

PDO()Its stands for PHP Data Objects. A set of PHP extensions that provides core PDO classes and database-specific drivers. Object-oriented programmers prefer PDO because of its compatibility with a large number of databases. Another important feature is database security. Both PDO and Mysqli provide SQL injection security. It helps to Protect databases and information from hackers and their attacks.

Creating a new database connection by using PDO.

$conn = “mysql:host=localhost;dbname=newdatabase;charset=utf8mb4”;

try {

$target = new PDO($conn, “username”, “password”);

} catch (Exception $abc) {

error_log($abc->getMessage());

exit();

}

 

PDO Supported Databases:

1. Mysql

2. Oracle

3. Postgresql

4. Ms SQL server

5. Sybase

6. Informix

7. IBM

8. Sqlite

9. Cubrid

10. Firebird

11. FreeTDS

12. 4D

 

Some users, particularly beginners, find PDO difficult to use because of syntax But PDO has many built-in statements and error handling is much easier than MySQL and Mysqli. PDO provides faster execution of multiple queries, security, and so on.