gpt4 book ai didi

php - 如何删除绑定(bind)结果上的 mysqli 警告?

转载 作者:行者123 更新时间:2023-11-29 14:19:59 25 4
gpt4 key购买 nike

我在错误报告中收到一条 mysqli 警告,指出警告:mysqli_stmt::bind_result() [mysqli-stmt.bind-result]: 绑定(bind)变量的数量与准备好的语句中的字段数量不匹配...第 36 行 有谁知道我如何正确绑定(bind)结果以便消除此警告?我真的不明白问题是什么,但我是一个使用 mysqli 的初学者。

下面是代码:

<?php
// PHP code
session_start();

//connected to db

// required variables (make them explciit no need for foreach loop)
$teacherusername = (isset($_POST['teacherusername'])) ? $_POST['teacherusername'] : '';
$teacherpassword = (isset($_POST['teacherpassword'])) ? $_POST['teacherpassword'] : '';
$loggedIn = false;

if (isset($_POST['submit'])) {

$teacherpassword = md5(md5("j3Jf92".$teacherpassword."D203djS"));

// don't use $mysqli->prepare here
$query = "SELECT * FROM Teacher WHERE TeacherUsername = ? AND TeacherPassword = ? LIMIT 1";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("ss",$teacherusername,$teacherpassword);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbTeacherForename,$dbTeacherSurname,$dbTeacherUsername,$dbTeacherPassword);

while($stmt->fetch()) {
if ($teacherusername == $dbTeacherUsername && $teacherpassword == $dbTeacherPassword) {
$loggedIn = true;
}
}

if ($loggedIn == true){
// left your session code as is - but think wisely about using
$_SESSION['teacherforename'] = $dbTeacherForename;
$_SESSION['teachersurname'] = $dbTeacherSurname;
header( 'Location: menu.php' ) ;
die();
}

/* close statement */
$stmt->close();

/* close connection */
$mysqli->close();
}
?>

最佳答案

不建议使用通配符 *。表格中的列数可能多于您需要的 4 列?

我会选择

SELECT TeacherForname, TeacherSurname, TeacherUsername, TeacherPassword FROM Teacher WHERE TeacherUsername = ? AND TeacherPassword = ? LIMIT 1

关于php - 如何删除绑定(bind)结果上的 mysqli 警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11903207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com