预备语句:变量数与预备语句中的参数数不匹配 [英] Prepared Statement: Number of variables doesn't match number of parameters in prepared statement

查看:92
本文介绍了预备语句:变量数与预备语句中的参数数不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题之前已经问过几次了,但是我的代码没有发现任何问题,但是我的代码仍然出现此错误,根本没有道理.

I know this question has been asked several times before but I don't see any problem with my code and yet I still get this error On my code which doesn't make sense at all.

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);

有什么我想念的吗?

任何帮助或建议,将不胜感激.

Any help or advice would be appreciated.

提前谢谢.

推荐答案

您尝试将2个参数绑定到没有任何参数的查询:

You try to bind 2 parameters to a query that does not have any parameters:

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

仅当您为参数定义占位符时才能绑定参数,

You can only bind parameters if you define placeholders for them like this:

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

?表示可以将参数绑定到的占位符.

The ? denotes placeholders you can bind parameters to.

这篇关于预备语句:变量数与预备语句中的参数数不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆