我正在从表单发送一组值。我想遍历数据库表来查找这些ID。当我收到这条消息时,我明白有些事情是错的......
致命错误:在第56行的/home/d15155/tool/pdf.php中调用非对象的成员函数bind_param()
if (count($_POST['q']) == 0){
}
else {
foreach($_POST['q'] as $quality){
# Prepare statement
$stmt = $mysqli->prepare("SELECT the_question, the_sub_questions, the_quality, the_time FROM my_questions WHERE the_category='2' AND the_headline='5' AND quality_id = ? ORDER BY the_sort_order ASC");
$stmt->bind_param('i', $quality);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($the_question, $the_sub_questions, $the_quality, $the_time);
$stmt->fetch();
$konkretaexempel .= utf8_encode($the_question) . " <br />";
}
}
我想将结果添加到一个长字符串中(然后在PDF中使用)。
修改
删除了foreach和数组,它仍然收到相同的错误消息。我检查过,数据库连接正常。
if (count($_POST['q']) == 0){
}
else {
$stmt = $mysqli->prepare("SELECT the_question, the_sub_questions, the_quality, the_time FROM my_questions WHERE the_category='2' AND the_headline='5' AND quality_id = ? ORDER BY the_sort_order ASC");
$stmt->bind_param('i', '27');
$stmt->execute();
$stmt->bind_result($the_question, $the_sub_questions, $the_quality, $the_time);
$stmt->fetch();
$konkretaexempel .= utf8_encode($the_question) . " <br />";
}
答案 0 :(得分:1)
肖恩在评论中的提示可能不仅仅是一个侧面说明;它将解决问题:每个连接只能有一个活动的查询/语句,在单个 - &gt; fetch()之后,语句仍处于活动状态(while循环会修复它,但这里不需要) 。当您按照建议重新使用$ stmt实例时,任何旧的结果集都将被丢弃。
您的脚本目前就像
<?php
$mysqli = setup();
if (count($_POST['q']) == 0){
myErrorHandling();
}
else {
foreach($_POST['q'] as $quality){
$stmt = $mysqli->prepare("SELECT x, y FROM soFoo WHERE id = ?");
if ( !$stmt ) { die('prepare failed'); }
$stmt->bind_param('i', $quality);
$stmt->execute();
$stmt->bind_result($x, $y);
$stmt->fetch();
printf("x=%d,y=%s\r\n", $x, $y);
}
}
function setup() {
// for demonstration purposes only
$_POST = [ 'q'=> [
1,3,5
]];
mysqli_report(MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'localonly', 'localonly', 'test');
$mysqli->query('
CREATE TEMPORARY TABLE soFoo (
id int auto_increment,
x int,
y varchar(32),
primary key(id)
)
');
$stmt = $mysqli->prepare('INSERT INTO soFoo (x,y) VALUES (?,?)');
$stmt->bind_param('ss', $x, $y);
foreach( range('a','z') as $x=>$y ) {
$stmt->execute();
}
return $mysqli;
}
,输出
x=0,y=a
prepare failed
现在,当我将调用/ bind_param调用到循环之前
<?php
$mysqli = setup();
if (count($_POST['q']) == 0){
myErrorHandling();
}
else {
$stmt = $mysqli->prepare("SELECT x, y FROM soFoo WHERE id = ?");
if ( !$stmt ) { die('prepare failed'); }
$stmt->bind_param('i', $quality);
foreach($_POST['q'] as $quality){
$stmt->execute();
$stmt->bind_result($x, $y);
$stmt->fetch();
printf("x=%d,y=%s\r\n", $x, $y);
}
}
function setup() {
... same as before...
}
输出
x=0,y=a
x=2,y=c
x=4,y=e
正如所料。