沃梦达 / 编程问答 / php问题 / 正文

mysqli_stmt::bind_param():类型定义字符串中的元素数与绑定变量数不匹配,无法选择

mysqli_stmt::bind_param(): Number of elements in type definition string doesn#39;t match number of bind variables and cannot select(mysqli_stmt::bind_param():类型定义字符串中的元素数与绑定变量数不匹配,无法选择)

本文介绍了mysqli_stmt::bind_param():类型定义字符串中的元素数与绑定变量数不匹配,无法选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我无法执行选择,抛出以下错误:

警告:mysqli_stmt::bind_result():绑定变量的数量与Login.php第19行的预准备语句中的字段数量不匹配

此代码:

$user = "user";
$pass = "pass";

$conn = new mysqli(MYSQL_HOST, MYSQL_USER,MYSQL_PASS,MYSQL_DB);

$sql = "SELECT * FROM users where pass = ? and user = ?" ;

$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $user, $pass);

$stmt->execute();
$stmt->bind_result($userLogin);
$stmt->fetch();

echo $userLogin;

这是MySQL中的表

id  int(11) NO  PRI     auto_increment
user    varchar(45) NO  UNI     
pass    varchar(45) NO          

我不明白为什么会出现错误

推荐答案

bind_result所有选定字段绑定到变量。这意味着如果从表中选择3个字段,必须提供3个变量:

$sql = "SELECT * FROM users where pass = ? and user = ?" ;

$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $user, $pass);

$stmt->execute();
// here - three fields are binded to three variables
$stmt->bind_result($userId, $userLogin, $userPass);
$stmt->fetch();

或仅选择必填字段:

$sql = "SELECT user FROM users where pass = ? and user = ?" ;

$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $user, $pass);

$stmt->execute();
// here - one field is binded to one variable
$stmt->bind_result($userLogin);
$stmt->fetch();

最后,您混合了bind_param变量的顺序:

$sql = "SELECT user FROM users where pass = ? and user = ?" ;
$stmt->bind_param("ss", $pass, $user); // first - pass, second - user

这篇关于mysqli_stmt::bind_param():类型定义字符串中的元素数与绑定变量数不匹配,无法选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:mysqli_stmt::bind_param():类型定义字符串中的元素数与绑定变量数不匹配,无法选择

基础教程推荐