2013-01-16 57 views
-1

Possible Duplicate:
Scope error - Call to a member function prepare() on a non-object致命錯誤:調用一個成員函數準備()一個非對象在/Applications/XAMPP/xamppfiles/htdocs/connect/includes/functions.php

我寫了這個代碼約2或3個月以前在我的Windows PC上。它運行..現在我在我的Mac上下載了xampp,並且出現以下錯誤:

致命錯誤:調用成員函數prepare()在/ Applications/XAMPP/xamppfiles/htdocs/connect /includes/functions.php在線39

// 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; 
    } 
+3

'renderTimeline'函數內部沒有'$ pdo'變量。 – AD7six

+0

它需要在我創建的訪問數據庫的每個函數中?有沒有更高效的方法? –

+0

只需將$ pdo傳遞給函數'function renderTimeline($ pdo){...' – Ronnie

回答

0

傳遞對象過你的函數,像這樣

function renderTimeline($pdo){ 

,並在你的代碼

$pdo = new pdo(); 
renderTimeline($pdo); 
相關問題