Ste Prescott
Ste Prescott

Reputation: 1817

PHP bind parameters $mysqli->prepare() statement not working

Hi I am not too sure what is wrong with what I have written. A friend has simular code but mine wont work. Would appreciate help.

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

Thanks

Upvotes: 1

Views: 1455

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270609

You have not fetched results with $stmt->fetch(). Although you have bound your result column to $postTitle, no value will be available unless you fetch a row from the statement result set.

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

Upvotes: 2

Related Questions