2012-01-07 111 views
1

嗨,我不太确定我写的是什么问题。朋友有相同的代码,但我的工作不会。希望能得到帮助。

if (isset($_GET['postID'])) 
{ 
    $postID = $_GET['postID']; 
    $stmt = $mysqli->prepare("SELECT postTitle FROM Posts WHERE postID = ?"); 
    $stmt->bind_param('i', $postID); 
    $stmt->execute(); 
    $stmt->bind_result($postTitle); 
    echo $postTitle; 
} 

感谢

+0

哦!非常抱歉!我的错误..误读帖子,有pdo记住:(删除评论.. – Nonym 2012-01-07 15:51:26

回答

2

您有$stmt->fetch()不读取的结果。尽管您已将结果列绑定到$postTitle,但除非您从语句结果集中获取一行,否则没有值可用。

// First, don't forget to establish your connection 
$mysqli = new MySQLi($host, $user, $pass, $dbname); 

if (isset($_GET['postID'])) 
{ 
    $postID = $_GET['postID']; 
    $stmt = $mysqli->prepare("SELECT postTitle FROM Posts WHERE postID = ?"); 
    $stmt->bind_param('i', $postID); 
    $stmt->execute(); 
    $stmt->bind_result($postTitle); 

    // Use a while loop if multiple rows are expected, 
    // Otherwise, a single call to $stmt->fetch() will do. 
    while ($stmt->fetch()) { 
    echo $postTitle; 
    } 
} 
+0

可悲的是,仍然没有工作的错误说致命错误:调用成员函数prepare()在/ Users/user/Sites中的非对象/php/post.php第29行 – 2012-01-08 02:51:17

+0

第29行是$ stmt = $ mysqli-> prepare(“SELECT postTitle FROM Posts WHERE postID =?”); – 2012-01-08 02:52:26

+0

这意味着您的连接$ mysqli未成功创建。新的mysqli(),对吗? – 2012-01-08 03:06:11