2012-12-14 48 views
-3
<?php 
$db = new PDO('mysql:host=localhost;dbname=dbname;charset=UTF-8', 'user', 'password'); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
if(!ctype_digit($_GET['id'])) 
{ 
    echo 'A news ID is required to access this page. <a href="/index.php">Return</a>'; 
} 
else 
{ 
    $article = $sql = "SELECT `title`,`content`,`date` FROM `news` WHERE `id` = ?"; 

    $q = $db->prepare($article); 
    $query->execute($article); 

    return $query; 
?> 

致命錯誤:調用一個成員函數EXEC()的一個非對象在/home/harold/public_html/news/index.php上管線147非對象上調用的成員函數的exec()

我絕對難住。我已經在這3天了。我不是最好的開發人員,所以任何幫助,非常感謝。

+0

你可以花一秒鐘格式化你的代碼,以便更容易爲人們閱讀? –

+0

我是一個巨大的noob,所以我可以嘗試,但我不認爲這會有所幫助。 –

+0

第一步,點擊帖子底部的「修改」。第二步:使用文本輸入區域頂部的格式化按鈕。新來的沒關係。歡迎。現在是時候成爲新的和有能力的人了。 – DavidO

回答

1

我不知道這行代碼147的線,但似乎你在這些線路的一個問題:

的所有變量名因某種原因發生變化
$q = $db->prepare($article); 
$query->execute($article); 

第一。其次,你需要發送你的ID爲傳遞給​​參數數組(你又通過查詢)

它應該是這樣的:

$stmt = $db->prepare($article); 
$success = $stmt->execute(array($_GET['id'])); 

第三,你在這裏,當你使用return甚至不在功能中。我不確定你想要做什麼。

如果你想與結果集,你需要做這樣的事情的聲明的工作:

if ($success === false) { 
    // the statement didn't execute for some reason 
    var_dump($stmt->errorInfo()); 
} else { 
    if ($stmt->rowCount() === 0) { 
     // your select didn't return any rows 
    } else { 
     // I am assuming that you id field is unique, so that you will only return 1 row at most, so I am not using any sort of looping construct 
     $news_object = $stmt->fetchObject(); 
     // alternately you can use this if you want an array -> $news_array = $stmt->fetch(); 
    } 
} 
相關問題