我收到此消息“ fatal error :在第 65 行对 C:\xampp\htdocs\pcms\includes\blog.php 中的非对象调用成员函数 bind_param()”,因为在我正在编写的脚本。
作为一名 PHP 新手(编码学习),我想知道是否可以在 while 循环中使用另一个准备好的语句中的准备好的语句。
如果有人能指出我哪里出错了/其他解决这个问题的方法(仍然使用准备好的语句),我将非常感激。
// 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
}
}
?>
非常感谢 吉姆
最佳答案
手册描述了bind_param第一个参数是“参数标识符。对于使用命名占位符的准备语句,这将是形式为:name的参数名称。对于使用问号占位符的准备语句,这将是1索引的参数的位置。”所以尝试使用
$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
因此,如果 PDO 无法创建准备好的语句,它将返回 false,而不是对象,这就是您所得到的。
关于php - fatal error : Call to a member function bind_param() PHP MySQL Prepared Statements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8273036/