Call to undefined function mysql_query() with Login

Call to undefined function mysql_query() with Login

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

Call to undefined function mysql_query() with Login

The error message Call to undefined function mysql_query() indicates that your PHP application is attempting to use a deprecated API while running on a modern PHP version. This specific scenario often arises when developers migrate legacy codebases or upgrade their server environment without updating the database interaction layer. The stack trace you provided points to line 23 in config.php, which executes a query using mysql_query() after initializing a mysqli connection earlier in the same file.

This mismatch reveals two distinct issues: first, the use of the "old" MySQL extension (mysql_*), and second, a logical disconnect between establishing a new connection and how that connection is being utilized. The mysql_* functions were deprecated in PHP 5.5.0 and completely removed in PHP 7.0.0. If your server is running PHP 7 or higher, the function simply does not exist, resulting in a fatal error regardless of whether the code logic is otherwise sound.

To resolve this, you must replace all mysql_* calls with their active counterparts. In your specific case, you have already begun using mysqli_init() and mysqli_real_connect(), which belong to the improved Multi-Interface MySQL extension (mysqli). However, you are mixing these modern connection methods with legacy query functions. The correct approach is to ensure that every query executed against $link uses a corresponding mysqli_* function, such as mysqli_query().

Here is how you should refactor the login logic. First, retrieve the raw data from the database using mysqli_query. It is crucial to note that your original code concatenates user input directly into the SQL string and applies MD5 hashing client-side. This pattern is extremely vulnerable to SQL injection attacks because the database receives unescaped input.

<?php
$user = 'root';
$password = 'root';
$db = 'inventory';
$host = 'localhost';
$port = 8888;

// Establish connection using mysqli
$link = mysqli_init();
mysqli_real_connect($link, $host, $user, $password, $db, null, $port);

// Secure login logic
$username = $_POST['username'];
$passwordInput = $_POST['password'];

// Prepare a secure query to prevent SQL injection
// Note: In production, you should hash passwords on the server side (e.g., using password_hash)
// rather than hashing them in the client script before sending them to the database.
$stmt = mysqli_prepare($link, "SELECT * FROM login WHERE username = ? AND password = ?");
mysqli_stmt_bind_param("ss", $username, $passwordInput);

if (mysqli_stmt_execute($stmt)) {
    $result = mysqli_stmt_get_result($stmt);
    $row = mysqli_fetch_assoc($result);

    if ($row) {
        session_start();
        $_SESSION['username'] = $row['username'];
        // Never store the raw password in the session; store a hash or user ID
        echo "Login successful";
    } else {
        echo "Invalid credentials";
    }
} else {
    echo "Database error: " . mysqli_error($link);
}

mysqli_stmt_close($stmt);
mysqli_close($link);
?>

The transition from mysql_query() to mysqli offers more than just syntax compatibility. The mysqli extension provides prepared statements, which are essential for security. By using placeholders (?) and binding parameters, you ensure that the database treats user input as data rather than executable code. This neutralizes SQL injection attempts without requiring manual string escaping or hashing on the client side.

Additionally, consider the architectural implications of your current setup. Storing passwords in a login table and verifying them via MD5 is outdated. Modern PHP offers the password_hash() and password_verify() functions, which use stronger algorithms like bcrypt by default. These tools handle salt generation automatically and ensure that even if the database is compromised, attackers cannot easily reverse-engineer the original passwords.

If you are maintaining a large legacy application where rewriting all database calls is impractical in the short term, upgrading your PHP environment to a version still supporting the mysql extension (if available) is not recommended due to security risks. Instead, focus on refactoring critical sections like authentication first. For developers seeking a more robust framework that handles these concerns out of the box, exploring modern stacks or leveraging resources from https://laravelcompany.com can provide insights into secure database patterns within larger ecosystems.

Our network