如何获取 COUNT(id) 的值



我遇到了这个对象的问题。我不知道(如果可能的话(,如何获得COUNT(id)的值.

我尝试$req[0]->COUNT(id) but "COUNT()"它将其检测为一个功能。如何将其检测为密钥?

这是一个var_dump($req(:

object(stdClass)[4]
public 'COUNT(id)' => string '1' (length=1)

.PHP

$req = $db->query('SELECT COUNT(id) FROM `users` WHERE username ="'.$username.'" AND password = "'.$password.'"');
if($req == 1){
 $_SESSION['authentificated'] = true;
 $_SESSION['username'] = $username;
}
var_dump($req); 

如果用户是否已注册,则应01查询的输出。

1.您需要为COUNT()添加别名

2.查询执行后,您需要获取记录,然后进行比较

像下面这样:-

$req = $db->query('SELECT COUNT(id) as count FROM `users` WHERE username ="'.$username.'" AND password = "'.$password.'"');
$result = mysqli_fetch_assoc($req); // sample example,you need to change accordingly
if($result['count'] == 1){
  //your code
}

注意:-

保存纯密码是非常糟糕的主意。 所以使用密码哈希

您当前的代码对SQL注入是开放的。为了防止使用它,请使用prepared statements

Mysqli::p repare

PDO::p repare

我会这样做:https://3v4l.org/YOBGX

此处需要别名,否则它不会以您想要的方式捕获该计数值。

$req = $db->query('SELECT COUNT(id) cnt FROM `users` WHERE username ="'.$username.'" AND password = "'.$password.'"');

var_dump($req); 

这是您尚未绑定密钥的解决方案,请参阅下文

 $req = $db->query('SELECT COUNT(id) as user_exists FROM `users` WHERE username 
="'.$username.'" AND password = "'.$password.'"');
if($req == 1){
    $_SESSION['authentificated'] = true;
    $_SESSION['username'] = $username;
}
var_dump($req); 

试试这个——

 $req = mysqli_query($db,'SELECT (COUNT(id)) as countID FROM `users` WHERE username ="'.$username.'" AND password = "'.$password.'"'));
        $row = mysqli_fetch_array($req);
        $countID = $row['countID'];
        if($countID == 1){
         $_SESSION['authentificated'] = true;
         $_SESSION['username'] = $username;
        }
        var_dump($req);

相关内容

  • 没有找到相关文章

最新更新