2015-07-12 60 views
0

我从窗体发送一个值的数组。我想遍历数据库表寻找这些ID。当我得到这个消息我明白了什么是错的...MySQLi foreach bind_param()

致命错误:调用一个成员函数bind_param()一个非对象在/home/d15155/tool/pdf.php在线56

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中使用)。

EDIT

删除了的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

它看起来像你截断了错误信息;您可能需要编辑您的问题以包含完整的讯息。 –

+0

这意味着'准备'不起作用。尝试在你的语句中添加\'aroung列名 – Random

+2

注意:' - > prepare()'的目的是让你不必在每次循环迭代时调用它。通过将它放入你的'foreach()'中,你正在浪费其最好的功能/特性之一。 – Sean

回答

1

肖恩在评论中的提示可能不仅仅是一个侧面说明,它将摆脱这个问题:每个连接只能有一个活动的查询/语句,并且在单一 - > 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 

现在,当我移动电话环路

<?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... 
} 

输出前的准备/ bind_param到是

x=0,y=a 
x=2,y=c 
x=4,y=e 

与预期的一样。

+0

感谢您的回复VolkerK,我尝试了您的建议,但我仍然收到相同的错误消息。我试图创建一个新的数组只是为了测试目的,但仍然是相同的错误。 – Mattias

+0

我已经用php 5.6.10/win32和mysql测试了那些脚本5.6.something – VolkerK

+0

嗯..我现在没有使用foreach和数组测试,仍然得到相同的错误...请参阅原文。 – Mattias