2016-02-11 87 views
0

我知道這個問題已經被問前幾次,但我不認爲我的代碼的任何問題,但我仍然得到這個錯誤在我的代碼這根本沒有意義。預處理語句:變量數不準備語句相匹配的參數個數

arning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in line 131 

這是我的全碼:

if($stmt = $db_conx->prepare("SELECT id, product_name FROM yt ORDER by id")) { 
    //die($db_conx->error); 

$product_list = ""; 
$stmt->bind_param("is", $id, $product_name); 
if (! $stmt->execute()) { 
    die($stmt->error); 
} 
$stmt->bind_result($id, $product_name); 
$stmt->store_result(); 

while($stmt->fetch()) { 
    $value[] = array('id'=>$id, 'product_name'=>$product_name); 
    $product_list .= "<li class='mix ".$product_name."' data-name='".$product_name."'><a href='../product_images/" . $id . "Image1.jpg'> 
             <img src='../product_images/" . $id . "Image1.jpg'></a> 
             <h4>".$product_name."</h4> 
             <br> 

            </li> 
            <div style='width:120px;'><a href='edit.php?edit=$id'>Edit this product</a></div> 
<br> 
<div style='width:120px;'><a href='products.php?delete=$id'>Delete this product</a></div>"; 

$files = glob('../cache/*'); // get all file names 
foreach($files as $file){ // iterate files 
    if(is_file($file)) 
    unlink($file); // delete file 
} 
    } 
} 
mysqli_stmt_close($stmt); 

而這正是上有行131:

$stmt->bind_param("is", $id, $product_name); 

在那裏,我失去了一些東西?

任何幫助或建議,將不勝感激。

在此先感謝。

回答

1

您嘗試2個參數綁定到沒有任何參數的查詢:

$stmt = $db_conx->prepare("SELECT id, product_name FROM yt ORDER by id"); 
$stmt->bind_param("is", $id, $product_name); 

如果定義佔位他們這樣只能綁定參數:

$stmt = $db_conx->prepare("SELECT id, product_name FROM where id = ? or product_name = ?"); 
$stmt->bind_param("is", $id, $product_name); 

?表示可以綁定參數佔位符。

+1

犯規這必須是SELECT ID,提供的PRODUCT_NAME YT其中id =?或product_name =? – rooz

+0

是啊,固定它,而你正在評論。請參閱此只是作爲一個例子可能不會做出太大的意義:-) – maxhb

-1

你要綁定的參數「是」,這不SQL語句 編輯存在:「?」和你沒有任何佔位符要麼

msqli_stmt::bind_param

+0

我認爲這是$ ID,$ PRODUCT_NAME是什麼! – rooz

+0

http://php.net/manual/en/mysqli-stmt.bind-result.php 綁定,結果從您在查詢中選擇兩列收集數據。它不會綁定動態查詢中的變量。看文檔的bind_param – lonewolf217

相關問題