Mysqli 预处理语句 stmt_num_rows returns 0
Mysqli prepared statement stmt_num_rows returns 0
出于某种原因,stmt_num_rows
总是以 0 结束。无论我使用正确的电子邮件和单词执行查询还是错误的。
这是代码:
include_once './Connect.php';
$db = new DB_Connect();
$con = $db->connect();
$password = $_POST["Password"];
$email = $_POST["Email"];
$word = $_POST["Word"];
$true = 'true';
$false = 'false';
$response = array();
$selectquery = mysqli_prepare($con, "SELECT ID FROM users WHERE Email = ? AND Word = ?");
mysqli_stmt_bind_param($selectquery , "ss" , $email , $word);
mysqli_stmt_execute($selectquery);
mysqli_stmt_store_result($selectquery);
mysqli_stmt_bind_result($selectquery, $ID);
if (mysqli_stmt_num_rows($selectquery) > 0){
$response[resp] = $true;
$updatequery = mysqli_prepare($con , "UPDATE users SET Password = ? WHERE Email = ? AND Word = ?");
mysqli_stmt_bind_param($updatequery ,"sss", $password, $email, $word);
mysqli_stmt_execute($updatequery);
mysqli_stmt_close($updatequery);
}else{
$response[resp] = $false;
}
echo json_encode($response);
mysqli_stmt_close($selectquery);
mysqli_close($con);
运行 下面的代码:密码、电子邮件 和Word从 android 发布。首先有一个查询来检查 table 中是否有一个用户(行)具有相应的 Email 和 Word。如果是,则添加 ID。然后调用 stmt_num_rows
来检查获取的行数,但获取的行数最终为 0(我得到的响应为 "false")。但如果它是一个,那么将调用 UPDATE 查询并且我将收到响应 "true"。有谁知道我犯的错误?
谢谢!
将我们的评论添加到答案中以结束此问题:
我的:
"Try removing / commenting those out and try it again. If that fails, then your POSTs may be failing. Check for errors against those and variables, and in your query."
和:
"also make sure that no whitespace is being introduced, if so, trim()
and do a var_dump. Remember that POST arrays are case-sensitive. Password
and password
etc. are two different animals. Same goes for the other POST arrays"
伊万的:
"Fred you were right the issue wasn't in the php it was actually the data posted. Since I'm using the Async Http library i forgot to post the actual parameters I want to send. I set the params to null"
密码
我注意到您可能以明文形式存储密码。不推荐这样做。
使用以下之一:
- CRYPT_BLOWFISH
crypt()
bcrypt()
scrypt()
- On OPENWALL
- PBKDF2
- PBKDF2 on PHP.net
- PHP 5.5 的
password_hash()
函数。
- 兼容包(如果PHP < 5.5)https://github.com/ircmaxell/password_compat/
其他链接:
关于列长度的重要旁注:
如果您决定使用 password_hash()
或 crypt,请务必注意,如果您当前的密码列的长度低于 60,则需要将其更改为(或更高) ).手册建议长度为255。
您需要更改列的长度并使用新的散列重新开始才能生效。否则,MySQL 将默默地失败。
出于某种原因,stmt_num_rows
总是以 0 结束。无论我使用正确的电子邮件和单词执行查询还是错误的。
这是代码:
include_once './Connect.php';
$db = new DB_Connect();
$con = $db->connect();
$password = $_POST["Password"];
$email = $_POST["Email"];
$word = $_POST["Word"];
$true = 'true';
$false = 'false';
$response = array();
$selectquery = mysqli_prepare($con, "SELECT ID FROM users WHERE Email = ? AND Word = ?");
mysqli_stmt_bind_param($selectquery , "ss" , $email , $word);
mysqli_stmt_execute($selectquery);
mysqli_stmt_store_result($selectquery);
mysqli_stmt_bind_result($selectquery, $ID);
if (mysqli_stmt_num_rows($selectquery) > 0){
$response[resp] = $true;
$updatequery = mysqli_prepare($con , "UPDATE users SET Password = ? WHERE Email = ? AND Word = ?");
mysqli_stmt_bind_param($updatequery ,"sss", $password, $email, $word);
mysqli_stmt_execute($updatequery);
mysqli_stmt_close($updatequery);
}else{
$response[resp] = $false;
}
echo json_encode($response);
mysqli_stmt_close($selectquery);
mysqli_close($con);
运行 下面的代码:密码、电子邮件 和Word从 android 发布。首先有一个查询来检查 table 中是否有一个用户(行)具有相应的 Email 和 Word。如果是,则添加 ID。然后调用 stmt_num_rows
来检查获取的行数,但获取的行数最终为 0(我得到的响应为 "false")。但如果它是一个,那么将调用 UPDATE 查询并且我将收到响应 "true"。有谁知道我犯的错误?
谢谢!
将我们的评论添加到答案中以结束此问题:
我的:
"Try removing / commenting those out and try it again. If that fails, then your POSTs may be failing. Check for errors against those and variables, and in your query."
和:
"also make sure that no whitespace is being introduced, if so,
trim()
and do a var_dump. Remember that POST arrays are case-sensitive.Password
andpassword
etc. are two different animals. Same goes for the other POST arrays"
伊万的:
"Fred you were right the issue wasn't in the php it was actually the data posted. Since I'm using the Async Http library i forgot to post the actual parameters I want to send. I set the params to null"
密码
我注意到您可能以明文形式存储密码。不推荐这样做。
使用以下之一:
- CRYPT_BLOWFISH
crypt()
bcrypt()
scrypt()
- On OPENWALL
- PBKDF2
- PBKDF2 on PHP.net
- PHP 5.5 的
password_hash()
函数。 - 兼容包(如果PHP < 5.5)https://github.com/ircmaxell/password_compat/
其他链接:
关于列长度的重要旁注:
如果您决定使用 password_hash()
或 crypt,请务必注意,如果您当前的密码列的长度低于 60,则需要将其更改为(或更高) ).手册建议长度为255。
您需要更改列的长度并使用新的散列重新开始才能生效。否则,MySQL 将默默地失败。