当我在另一个文件中调用函数时执行 mysql 命令时出现问题。如果我在同一个文件中有代码,它工作正常。代码如下所示:
认证:
{
$conn = new mysqli($dbserver, "dbuser", "pass", $dbname);
$sql = 'SELECT userId, salt, pwd, isAdmin FROM users where username = ?';
$stmt = $conn->stmt_init();
$stmt->prepare($sql);
$stmt->bind_param('s', $username);
$stmt->bind_result($userId, $salt, $storedPwd, $isAdmin);
$stmt->execute();
$stmt->fetch();
}
现在,如果我在 fetch 之后立即调用一个函数,它会在 bind_param 行上失败。我这样称呼它:
updateUserActivity ($conn, $tpcLang, 'LI', $userId);
这个函数的开头是:
function updateUserActivity ($conn, $lang, $activityType, $userId)
{
// check that activity is valid
$sql = 'SELECT activityType
FROM activityType
WHERE activityType = ?';
$stmt = $conn->stmt_init();
$stmt->prepare($sql);
$stmt->bind_param('s', $activityType);
$stmt->bind_result($activityTypeInDB);
$stmt->execute();
$stmt->fetch();
if ($activityType == $activityTypeInDB){
$success=1;
} else {
$success=0;
$msg = $lang->get('UPDATE_USER_INVALID_ACTIVITY_ERROR_MSG') . " " . $activityType . " (" . $stmt->errno . "): " . $stmt->error;
error_log ($msg);
}
// continue on to add a row in the userActivity table
}
我在 bind_param 上得到的错误是:无效的对象或资源 mysqli_stmt,而数据库错误是:命令不同步;您现在无法运行此命令。
任何建议将不胜感激。