php - 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():: 语句中的元素数量与绑定参数的数量不匹配”。
解决方案
你应该使用准备
$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";
}
?>
推荐阅读
- apollo-server - 使用 Key 启动网关以验证配置,然后使用 Bearer 令牌进行查询
- c++ - 首先调用复制构造函数,而不是仅在传递 R 值引用时移动构造函数
- python - 排序列表的实现类
- javascript - 在onClick上从另一个php将两个变量返回到两个不同的div
- linux - Linux:连接时的 USB 触发器仅在首次连接时触发。需要 `systemd --user damon-reload` 再次激活
- static-libraries - nim:使用静态库
- algorithm - 有人可以评估这个函数的时间复杂度吗
- google-cloud-platform - Bigquery DML 操作
- swift - 教程类型“NSNotification.Name”没有成员“UIResponder”中的 Swift 5 不起作用
- java - 使用 Java 将具有时区偏移的日期和时间转换为时间戳