首页 > 解决方案 > PHP准备语句错误“绑定参数的数量与准备语句中的字段数量不匹配

问题描述

我试图使用准备好的语句进行查询。代码如下

<?php
$studentrollno=1;
$studentclass=10;
$studentsection='A';
$host="localhost";
$dbName="school_election_db"
    $conn=new mysqli_connect($host,dbName); 
    if(conn->connect_error())
    {
        echo "error occured";
    }
    else
    {
        $stmt="SELECT * FROM voting_details where studentrollno=? and 
    studentclass=? and studentsection=?";
    $conn->bind_param($studentrollno,$studentclass,$studentsection);
    $result=$conn->execute();
    if(result==true)
    {
    echo "login succesfull";
    }
    else
    {
    echo "Please try again";
    }
    ?>

错误在 mysqli 查询周围,但我无法找出错误。当我使用带有过程 PHP 的普通语句时,它可以正常工作。但我读到正常的方法是使用 OOP 和准备好的语句。我得到的错误是“mysqli_bind_param():: 语句中的元素数量与绑定参数的数量不匹配”。

标签: phpmysqli

解决方案


你应该使用准备

$dbName="school_election_db"
$conn=new mysqli_connect($host,dbName); 
if(conn->connect_error())
{
    echo "error occured";
}
else
{
    $stmt=  $conn->prepare("SELECT * 
        FROM voting_details 
        where studentrollno = ? 
        and  studentclass = ?
      and studentsection = ? ") ;
$stmt->bind_param('iis',$studentrollno,$studentclass, $studentsection);
$result=$stmt->execute();
if($result==true)
{
echo "login succesfull";
}
else
{
echo "Please try again";
}
?>

推荐阅读