1

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

Ste Prescott
  • 1,789
  • 2
  • 22
  • 43
  • Oh! Terribly sorry! My mistake.. Misread the post, had pdo in mind :( Deleting comment.. – Nonym Jan 07 '12 at 15:51

1 Answers1

2

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;
    }
}
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • Sadly that still didnt work the error says Fatal error: Call to a member function prepare() on a non-object in /Users/user/Sites/php/post.php on line 29 – Ste Prescott Jan 08 '12 at 02:51
  • line 29 is $stmt = $mysqli->prepare("SELECT postTitle FROM Posts WHERE postID = ?"); – Ste Prescott Jan 08 '12 at 02:52
  • That would mean your connection $mysqli was not successfully created. You did call new mysqli(), right? – Michael Berkowski Jan 08 '12 at 03:06
  • Thanks for pointing that out to me. I didn't make a new instance of $mysqli I now have a problem as it is not getting the content. It retrieves every other field, just not the content. – Ste Prescott Jan 08 '12 at 15:28
  • @user1136076 You'll need to post your code, possibly as a new question. I can't deduce what you mean from your comment. – Michael Berkowski Jan 08 '12 at 15:32