PHP Prepared语句错误"绑定参数的数量与准备好的语句

时间:2018-05-04 14:52:50

标签: php mysqli

我试图为查询使用预准备语句。 代码如下

<?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和准备好的陈述 我得到的错误是&#34; mysqli_bind_param()::语句中的元素数量与绑定参数的数量不匹配&#34;。

1 个答案:

答案 0 :(得分:3)

你应该使用准备

$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";
}
?>