Uncaught Error: Call to undefined function mysql_select_db()

Uncaught Error: Call to undefined function mysql_select_db()

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

Stefan Bogdanescu

Founder & Senior Architect

Uncaught Error: Call to undefined function mysql_select_db()

The error message Fatal error: Uncaught Error: Call to undefined function mysql_select_db() is a classic sign that your PHP code is running on a modern server environment while relying on legacy database functions. The stack trace points directly to line 10 in your fetch.php file, specifically where you attempt to select the database using mysql_select_db().

This error occurs because the original MySQL extension (mysql_*) was deprecated in PHP 5.5 and completely removed in PHP 7.0. If you are developing on XAMPP or any modern stack, your PHP version is likely 7.4 or higher, making the mysql_ functions unavailable. The solution isn't to reinstall an old library; it is to migrate your code to the mysqli extension, which offers improved functionality and security features like prepared statements.

Migrating from mysql_ to mysqli

Your current script attempts to connect using mysqli_connect() but then immediately tries to use the old mysql_select_db() function with that handle. This mix-and-match approach causes the fatal error because you are starting a connection in the new style but trying to execute queries in the deprecated style.

To fix this, you must consistently use the mysqli API throughout your script. The process of selecting a database in mysqli differs slightly from the old method; you do not need a separate function call. Instead, you pass the desired database name directly into the connection object when querying or executing statements.

Here is how you should refactor the connection and selection logic:

<?php  
$username = "root";  
$password = "namungoona";  
$hostname = "localhost";  

// Establish the connection
$dbhandle = new mysqli($hostname, $username, $password); 

if ($dbhandle->connect_error) {
    die("Connection failed: " . $dbhandle->connect_error);
}

// No need for mysql_select_db(). 
// We can select the database by setting it as the default for this connection.
$dbhandle->select_db("police");  

// Now use mysqli_query, not mysql_query
$result = $dbhandle->query("SELECT * FROM News");

$json_response = [];

if ($result) {
    // fetch data in array format
    while ($row = $result->fetch_assoc()) {
        $row_array['Headlines'] = $row['Headlines'];  
        $row_array['Details'] = $row['Details']; 
        $row_array['NewsPhoto'] = $row['NewsPhoto']; 
        
        // Push the values in the array
        $json_response[] = $row_array;
    }
}

echo json_encode($json_response);  
?>

Object-Oriented vs. Procedural Styles

The code above uses the object-oriented style of mysqli, which is generally preferred for readability and error handling. However, if your project relies heavily on procedural syntax, you can still migrate without switching to classes entirely. You would simply change the function calls:

  1. Replace mysql_connect() with mysqli_connect().
  2. Replace mysql_select_db() with $dbhandle->select_db('database_name').
  3. Replace mysql_query() with mysqli_query($dbhandle, 'SQL_string').
  4. Replace mysql_fetch_array() with mysqli_fetch_assoc() or mysqli_fetch_array().

The key takeaway is consistency. You cannot instantiate a mysqli connection and expect it to work with mysql_ functions. The underlying C extensions are different, and the function signatures do not match.

Security Considerations

Beyond fixing the fatal error, switching to mysqli allows you to address significant security flaws present in your original code. Your current script uses string concatenation for the SQL query: "select * from News". While this specific query is static and safe, real-world applications often build queries dynamically based on user input. If a variable were inserted directly into that string without sanitization, it would be vulnerable to SQL injection attacks.

The mysqli extension supports prepared statements, which separate the SQL logic from the data. This ensures that user inputs are treated strictly as data and never as executable code. For example, fetching specific news based on an ID would look like this:

$id = $_GET['id']; // Example input
$stmt = $dbhandle->prepare("SELECT * FROM News WHERE id = ?");
$stmt->bind_param("i", $id); // 'i' indicates an integer parameter
$stmt->execute();
$result = $stmt->get_result();

Moving to Laravel

If you are maintaining a legacy PHP script with numerous mysql_ calls or mixing deprecated and modern functions, the technical debt may be too high to refactor manually. In such cases, migrating to a modern framework is often the most pragmatic decision.

Laravel provides robust database abstraction layers that completely eliminate the need for writing raw SQL strings or managing connection handles manually in this manner. The Eloquent ORM handles database interactions seamlessly and enforces security best practices by default. If you are looking for guidance on how to structure your next project with modern PHP standards, https://laravelcompany.com offers resources on building scalable applications without falling back into legacy patterns.

By updating your code to use mysqli consistently and adopting prepared statements, you resolve the immediate fatal error while simultaneously securing your application against common vulnerabilities.

Our network