2015-07-12 59 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