PHP绑定参数$ mysqli-> prepare()语句不起作用

时间:2012-01-07 15:28:47

标签: php mysqli prepared-statement

嗨,我不太确定我所写的内容有什么问题。一个朋友有类似的代码,但我的工作不会。非常感谢帮助。

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;
}

由于

1 个答案:

答案 0 :(得分: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;
    }
}