Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
721 views
in Technique[技术] by (71.8m points)

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

I am trying to access a list of categories and their contents. I have a class called Categories. I keep getting this error. The weird thing is that I've used this same exact code in two other places so far with no problems. All I did here was reuse the code and change all the variables.

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

Here is the code to 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();
    }
}

?>

And here is the code I am 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');
?>
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like your $pdo variable is not initialized. I can't see in the code you've uploaded where you are initializing it.

Make sure you create a new PDO object in the global scope before calling the class methods. (You should declare it in the global scope because of how you implemented the methods inside the Category class).

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...