【问题标题】:PHP Prepared statement error "Number of bind parameters do not match number of number of fields in prepared statementPHP 准备语句错误“绑定参数的数量与准备语句中的字段数量不匹配
【发布时间】:2018-10-15 02:33:00
【问题描述】:

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

<?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():: The number of elements in the statement does not match the number of bound parameters”。

【问题讨论】:

  • ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 添加到脚本顶部。
  • 您在此处列出的代码充满了语法错误。您可能希望显示给出错误的确切代码。
  • 我不知道你是怎么得到这个错误的。您应该收到该代码的解析错误。
  • 您的代码有很多语法错误。我们应该假设这只是错误的复制粘贴吗?
  • 我已经编辑了所有内容..我希望现在一切都是正确的

标签: php mysqli


【解决方案1】:

你应该使用准备

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

【讨论】:

  • 可能想要解决语法错误 -> and studentclass = ?"; and studentsection = ? ") ";
  • 您缺少绑定类型:bool mysqli_stmt::bind_param ( string $types , mixed &amp;$var1 [, mixed &amp;$... ] )
  • 这不是编码服务..查看代码..似乎正确但只有您可以检查是否工作正常
  • @aynber 也添加了参数类型
  • 是你的代码 OOP 风格。我问是因为在绑定参数部分你已经写了 PDO:PARAM_INT。很抱歉问这些蹩脚的问题。我是初学者 :)
猜你喜欢
  • 2016-05-22
  • 2022-12-05
  • 1970-01-01
  • 2017-06-25
  • 1970-01-01
  • 2022-12-21
  • 2014-01-06
  • 2023-03-26
相关资源
最近更新 更多