-1

可能重复:
范围错误 - 在非对象上调用成员函数 prepare()

大约 2 或 3 个月前,我在我的 Windows PC 上编写了这段代码。它运行..现在我在我的 Mac 上下载了 xampp,我收到以下错误:

致命错误:在第 39 行的 /Applications/XAMPP/xamppfiles/htdocs/connect/includes/functions.php 中的非对象上调用成员函数 prepare()

// Connect to the database
    try {
        $pdo = new PDO('mysql:host=localhost;dbname=mo33', 'root', '');
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    }catch (PDOException $e){
        $error = "There was an issue connecting to the database: \n" + $e->getMessage();
        include 'html/error.html.php';
        exit();
    }

function renderTimeline(){

        try {
            $sql = 'SELECT user_publications.id, user.firstname, user.lastname, user.thumbnail, article, date_added
                    FROM user_publications INNER JOIN user 
            ON user_id = user.id 
            ORDER BY user_publications.id DESC';
            $s = $pdo->prepare($sql);   ---------------------------LINE 39--------------- 
            $result = $pdo->query($sql);
        }catch(PDOException $e){
            $output = 'There was an error while finding posts in the database..: ' . $e->getMessage();
            include 'html/error.html.php';
            exit();
        }

        while($row = $result->fetch()) {
            $user_publications[] = array(
        'id'=>$row['id'], 
        'name'=>$row['firstname'] + " " + $row['lastname'], 
        'thumbnail'=>$row['thumbnail'], 
        'article'=>$row['article'], 
        'date'=>$row['date_added']);
        }
        foreach ($user_publications as $post) {
            renderUserPublication($post['id'], $post['name'], $post['thumbnail'], $post['article'], $post['date']);
        }

        return;
    }
4

1 回答 1

0

像这样传递对象你的函数

function renderTimeline($pdo){

在你的代码中

$pdo = new pdo();
renderTimeline($pdo);
于 2013-01-16T01:09:37.593 回答