0

I am receiving this message "Fatal error: Call to a member function bind_param() on a non-object in C:\xampp\htdocs\pcms\includes\blog.php on line 65" for using the following code in a script I am working on.

As a bit of a PHP novice (coding to learn) I would like to know if it is not possible to use a prepared statement inside a while loop from another prepared statement.

If anybody could point out where I am going wrong/other ways of going about this (still using prepared statements) I would be very grateful.

// Load the blog data from the database and display a row for each blog post
$result = $dbc->prepare("SELECT id, title, post, date, time, blog, b_id, name, comment FROM blog ORDER BY id DESC LIMIT $start_blog , $blog_per_page");
$result->execute(); 
$result->bind_result($id, $title, $post, $date, $time, $blog, $b_id, $name, $comment);
<?php
while ($row = $result->fetch()) {
  if ($blog == 1) {
    // Count the number of comments for the current blog post
    $count = $dbc->prepare("SELECT COUNT( b_id ) FROM blog WHERE b_id = ?");
    $count->bind_param('s', $id); <-- Line 65
    $count->execute();
    $count->store_result();
    $no_comments=$count->num_rows;
    ?>
    <div style="margin-top:15px; margin-bottom:15px; border:1px solid #444444; padding:7px;">
      <div style="float:left; width:50%;"><b><?php echo $title; ?></b></div>
      <div style="float:right; text-align:right; width:50%;"><?php echo $date; ?> at <?php echo $time; ?></div>
      <div style="width:100%; padding-top:25px;"><?php echo nl2br(substr($post, 0, 100));?><?php if (strlen($post) >= 100); { echo '...'; } ?></div>
      <div style="width:100%; padding-top:25px;"><?php echo "<a href=\"$page-p-$id.html\" title=\"View comments for $title\"><i>Comments ($no_comments)</i></a> "; ?></div>
    </div>
<?php
  }
}
?>

Many thanks Jim

TheVDM
  • 43
  • 1
  • 1
  • 4

1 Answers1

1

Manual describes that bind_param first argument is "Parameter identifier. For a prepared statement using named placeholders, this will be a parameter name of the form :name. For a prepared statement using question mark placeholders, this will be the 1-indexed position of the parameter." so try use

$count->bind_param(1, $id);
//or 
$count = $dbc->prepare("SELECT COUNT( b_id ) FROM blog WHERE b_id = :named");
$count->bind_param(':named', $id); 

http://www.php.net/manual/en/pdostatement.bindparam.php

so if PDO cant create prepared statement it will return false, not an object, thats what you are getting.

Aurimas Ličkus
  • 9,886
  • 4
  • 24
  • 26
  • HI, unfortunatly both those options still return the same error, I have a feeling that the error is caused as there is still a bind_param/execute going in the while statement. But then I am still learning and as such probably wrong in my thinking – TheVDM Nov 25 '11 at 18:52
  • I have created a work around for now, unfortunatly it envolves moving away from prepared statements for this one call (will have to remember all the security precautions). – TheVDM Nov 25 '11 at 19:30