2012-12-14 45 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(); 
    } 
} 
相关问题