2011-11-25 79 views
0

我收到此消息“致命错误:调用一个成员函数bind_param()的非对象在C于:\ XAMPP \ htdocs中\ PCMS \第65行包含\ blog.php“,以便在我正在处理的脚本中使用以下代码。致命错误:调用一个成员函数bind_param()PHP MySQL的准备语句

带有一点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 
    } 
} 
?> 

非常感谢 吉姆

回答

1

手册描述了bind_param第一个参数是“参数标识符对于使用命名占位符一份声明中,这将是形式的参数名:名预处理语句使用问号占位符,这将是参数的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,而不是一个对象,那你做了什么。

+0

HI,不幸的是这两个选项仍然返回相同的错误,我有一种感觉,因为仍有bind_param错误引起/执行要在while语句。但后来我仍然在学习,因此可能是错在我的思想 – TheVDM

+0

我创建了一个变通现在,不幸的是它envolves移动从这个调用准备的语句远(必须记住所有的安全预防措施)。 – TheVDM

相关问题