简体   繁体   English

如何从PHP获取SQL中受影响的行

[英]How to get the affected rows in sql from php

I have written down the following code..But when I try to get the affected rows (to check if the user details matches the database's) it ALWAYS return 0 as affected rows even if the statement is executed successfully..What have I done wrong? 我写下了以下代码。但是,当我尝试获取受影响的行(以检查用户详细信息是否与数据库的行匹配)时,即使语句成功执行,它也总是返回0作为受影响的行。.我做错了什么?

if($connection){

$username = "admin";
$password = "admin";

$statement = mysqli_prepare($connection,"SELECT * FROM  `admin` WHERE username = ? AND password = ?");

mysqli_stmt_bind_param($statement,"ss",$username,$password);
mysqli_stmt_execute($statement);

if(mysqli_affected_rows($connection) == 1){
    $response = array('response'=>true);
}else{
    $response = array('response'=>false);
}

echo json_encode($response);

mysqli_stmt_close($statement);
mysqli_close($connection);     }

Use the mysqli_stmt_affected_rows() function, that takes a statement as the parameter: 使用mysqli_stmt_affected_rows()函数,该函数将一条语句作为参数:

Change 更改

if(mysqli_affected_rows($connection) == 1)

to

if(mysqli_stmt_affected_rows($stmt) != 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM