2012-01-07 62 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