Home  >  Q&A  >  body text

Fatal error: Calling member function prepare() on a null object

I'm trying to access a list of categories and their contents. I have a class called "Category". I keep getting this error. The strange thing is that I've used the same code in two other places so far without any problems. All I did here was reuse the code and change all the variables.

Fatal error: Call to a member function prepare() on null

This is the code of my class:

<?php

class Category {
    public function fetch_all() {
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM dd_cat");
        $query->execute();

        return $query->fetchAll();
    }

    public function fetch_data($cat_id) {
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM dd_cat WHERE cat_id = ?");
        $query->bindValue(1, $cat_id);
        $query->execute();

        return $query->fetch();
    }
}

?>

This is the code I'm trying to call:

<?php
session_start();
//Add session_start to top of each page//
require_once('includes/config.php');
require_once('includes/header.php');
include_once('includes/category.php');

?>
<link rel="stylesheet" href="css/dd.css">
    <div id="menu">
        <a class="item" href="drop_index.php">Home</a> -
        <a class="item" href="create_topic.php">Create a topic</a> -
        <a class="item" href="create_cat.php">Create a category</a>
        <div id="userbar">
<?php
    if( $user->is_logged_in() )
    {
        echo 'Hello ' . $_SESSION['user_name'] . '. Not you? <a href="logout.php">Sign out</a>';
    }
    else
    {
        echo '<a href="login.php">Sign in</a> or <a href="index.php">create an account</a>.';
    } 
?>
        </div>
    </div>

<?php

$category = new Category;
$categories = $category->fetch_all();

?>
    <div id ="wrapper">
        <h1>Categories</h1>
        <section>
            <ul>
                <?php foreach ($categories as $category) { ?>
                    <li><a href="category.php?id=<?php echo $category['cat_id']; ?>">
                        <?php echo $category['cat_title']; ?></a> 
                    </li>
                <?php } ?>
            </ul>
        </section>
    </div>
<?php
require_once('includes/footer.php');
?>


P粉824889650P粉824889650344 days ago604

reply all(2)I'll reply

  • P粉373990857

    P粉3739908572023-10-15 12:07:07

    In ---- model: Add use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

    Change class ----- extends Model to class ----- extends Eloquent

    reply
    0
  • P粉416996828

    P粉4169968282023-10-15 09:50:08

    Looks like your $pdo variable has not been initialized yet. I don't see where you initialize it in the code you uploaded.

    Be sure to create a new PDO object in the global scope before calling a class method. (Due to the way you implemented the method in the Category class, you should declare it in the global scope).

    $pdo = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

    reply
    0
  • Cancelreply