【问题标题】:Prepared Statement: Number of variables doesn't match number of parameters in prepared statement准备好的语句:变量的数量与准备好的语句中的参数数量不匹配
【发布时间】:2026-01-28 12:40:01
【问题描述】:

我知道这个问题已经被问过好几次了,但我没有发现我的代码有任何问题,但我的代码仍然出现这个错误,这根本没有意义。

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

我有什么遗漏的吗?

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

提前致谢。

【问题讨论】:

    标签: php mysql mysqli


    【解决方案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);
    

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

    【讨论】:

    • 这不是必须是 SELECT id, product_name FROM yt where id = 吗?或 product_name = ?
    • 是的,在您发表评论时已修复。请将此仅视为一个可能没有太大意义的示例:-)
    【解决方案2】:

    您正在绑定的参数“is”在您的 sql 语句中不存在 编辑:你没有任何“?”占位符

    msqli_stmt::bind_param

    【讨论】: