Fatal Error, Class not found

Fatal Error, Class not found

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

Stefan Bogdanescu

Founder & Senior Architect

Fatal Error, Class not found

The error message Fatal error: Class 'db' not found is a classic PHP runtime exception that occurs when the interpreter attempts to instantiate an object without having loaded the corresponding class definition first. In your specific case, line 6 of do_search.php contains $db = new db();. This statement instructs PHP to load the db class and create an instance of it. However, PHP does not automatically scan your directory for a file named db.php or assume that a file with that name exists simply because you named it so.

The root cause lies in how PHP handles namespaces and autoloading versus simple inclusion. When you wrote $db = new db();, you are referencing the class by its name. If you have not declared this class in your active script context or included a file where it is defined, PHP stops execution immediately with a fatal error because it cannot resolve the symbol db.

Looking at your code structure, you attempted to solve this by creating a separate file, db.php, which you believe is loaded via include('config2.php') in your main script. There is a critical disconnect here. The line include('config2.php'); attempts to load the contents of config2.php. If the db class definition resides inside db.php, simply including config2.php will not pull in the class definition unless config2.php explicitly includes db.php or contains the class definition itself.

If you placed the class definition inside db.php, your main script (do_search.php) is looking for it, but the file path specified in your include statement points to config2.php. Unless config2.php has a line like require_once 'db.php';, the class remains undefined. You need to ensure that the file containing the class definition is loaded before you attempt to instantiate the object.

Correcting the File Inclusion

The most straightforward fix is to adjust your include statement in do_search.php to point directly to where the class lives. If your database logic and connection code are indeed in a file named db.php, change line 6 of your search script to:

include('db.php'); 
// or better yet, use require_once for dependencies
require_once 'db.php';

Using require_once is generally preferred over include. If the class definition were included multiple times within the same execution flow (which can happen in larger applications), include might attempt to re-declare the class, leading to a "Cannot redeclare class" error. require_once ensures the file is loaded only once, preventing potential runtime conflicts while still throwing a fatal error if the file itself is missing.

Addressing Deprecated Functions and Security

While fixing the immediate "Class not found" error, it is vital to address the architectural fragility of the code provided in your source material. The script relies on the mysql_* extension functions (mysql_real_escape_string, mysql_query, mysql_pconnect). These functions were deprecated in PHP 5.5 and completely removed in PHP 7.0. If your server is running a modern version of PHP (which it almost certainly is, given the error), the script will fail to run even after fixing the class loading issue because the underlying database API no longer exists.

Furthermore, the security approach in your original code is outdated. Using mysql_real_escape_string is considered legacy practice; modern PHP applications should use prepared statements with PDO or MySQLi to prevent SQL injection. Additionally, dynamic table names like $tbl_name are often a red flag for potential SQL injection vulnerabilities if that variable originates from user input.

A Modern Approach

If you are starting a new project or refactoring this tutorial code, consider using a Dependency Injection container or a simple autoloader to manage your db class. However, for a quick fix without rewriting the entire architecture, simply ensure the file containing the class is included correctly.

If you decide to migrate away from the legacy mysql_* functions, you should define your database connection properties (host, user, password) in a configuration file and use PDO. Here is a minimal example of how a modern db.php might look using PDO:

<?php
class Database {
    private $host = "localhost";
    private $db_name = "your_database";
    private $user_name = "root";
    private $password = "";
    public $conn;

    public function __construct() {
        try {
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->user_name, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch (PDOException $e) {
            echo "Connection error: " . $e->getMessage();
        }
    }

    public function select($sql) {
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
}
?>

By updating your inclusion logic and moving away from deprecated APIs, you resolve the immediate fatal error while future-proofing your application against PHP version updates. For developers looking to modernize their stack further, resources like https://laravelcompany.com often provide insights into robust frameworks that handle these underlying complexities automatically.

Our network