简体   繁体   English

如何获取 SQL 选择查询返回的行数?

[英]How to get number of rows returned by an SQL select query?

I would like to get the number of lines returned by a select query in PHP.我想获取 PHP 中选择查询返回的行数。 I have the following code:我有以下代码:

$connection = new mysqli($server_name, $server_login, $server_password, $dbName);

if (!$connection) {
    echo "error";
    die("Connection failed. ".mysqli_connect_error())
}

//...

$command = "SELECT player_id FROM Player WHERE player_login = '" . $login."' AND player_password= '".$password."' ";

$result = mysqli_query($connection, $command);
echo num_rows($result);

I also tried with mysqli_stmt_num_rows() and mysqli_num_rows() but my result is always null (no result actually).我也尝试过mysqli_stmt_num_rows()mysqli_num_rows()但我的结果总是为空(实际上没有结果)。

Do you know why?你知道为什么吗?

There are a few ways to get the number of rows returned, the most common ones are to run COUNT(*) in MySQL, but there's also mysqli_num_rows($result) (not num_rows() like you used, unless you created that function yourself).有几种方法可以获取返回的行数,最常见的方法是在 MySQL 中运行COUNT(*) ,但也有mysqli_num_rows($result) (不是您使用的num_rows() ,除非您自己创建了该函数) )。 mysqli_stmt_num_rows() will only work when you're using prepare() instead of query() . mysqli_stmt_num_rows()仅在您使用prepare()而不是query()时才有效。

In ordre to use COUNT(*) you have to run and fetch the query first, while mysqli_num_rows() is a constant returned by the MySQLiResult object, which you can use if the query didn't fail.为了使用COUNT(*)您必须首先运行并获取查询,而mysqli_num_rows()是由 MySQLiResult 对象返回的常量,如果查询没有失败,您可以使用它。

I modified the piece of code you've got to check if the query actually succeeded, mysqli_num_rows() won't work if the query failed.我修改了一段代码,你必须检查查询是否真的成功,如果查询失败, mysqli_num_rows()将不起作用。

$command = "SELECT player_id FROM Player WHERE player_login = '" . $login."' AND player_password= '".$password."' ";

if ($result = mysqli_query($connection, $command)) {
    echo mysqli_num_rows($result);
} else {
    /* Query failed */
    echo "There was an error with the query: $command";
    echo "<br />".mysqli_error($connect);
}

Or you can use COUNT(*) , but then you'll have to fetch the results first.或者您可以使用COUNT(*) ,但是您必须先获取结果。

$command = "SELECT player_id, COUNT(*) as cnt FROM Player WHERE player_login = '" . $login."' AND player_password= '".$password."' ";

if ($result = mysqli_query($connection, $command)) {
    $row = mysqli_fetch_assoc($result);
    echo $row['cnt'];
} else {
    /* Query failed */
    echo "There was an error with the query: $command";
    echo "<br />".mysqli_error($connect);
}

You should also note that this query is vulnerable to SQL injection, you should learn how to use prepared statements with placeholders to protect yourself against that.您还应该注意,此查询容易受到 SQL 注入的影响,您应该学习如何使用带有占位符的准备好的语句来保护自己免受这种情况的影响。 The manual on prepare() is a good place to start with that. prepare()手册是一个很好的起点。


You also seem to be storing passwords either in plain-text, or with poor methods (such as md5 or sha1 ).您似乎还以纯文本或糟糕的方法(例如md5sha1 )存储密码。 PHP offer's a built-in function, password_hash() / password_verify() which you should use. PHP 提供了一个内置函数,您应该使用password_hash() / password_verify() If you're below PHP version 5.5, these functions aren't native, but there's a compability pack which can be used instead.如果您的 PHP 版本低于 5.5,则这些函数不是本机的,但可以使用兼容包。

As a final note, mixing object oriented and procedural code will technically work (as the procedural ones in reality call the object oriented ones), but it's considered bad practice.最后要注意的是,混合面向对象和过程代码在技术上是可行的(因为过程代码实际上称为面向对象代码),但它被认为是不好的做法。 If you connect with an object, continue to use object-oriented code.如果与对象连接,请继续使用面向对象的代码。

References参考

$command = "SELECT count(*) as numberofrecord, player_id FROM Player WHERE player_login = '" . $login."' AND player_password= '".$password."' ";

Very simple solution:-非常简单的解决方案:-

Use $result->num_rows in below way:-按以下方式使用$result->num_rows :-

if ($result = $mysqli->query("SELECT player_id FROM Player WHERE player_login = '" . $login."' AND player_password= '".$password."'")) {
  printf("Select returned %d rows.\n", $result->num_rows);
}

Reference:- http://php.net/manual/en/mysqli.query.php参考:- http://php.net/manual/en/mysqli.query.php

Note:-笔记:-

Meanwhile read prepared statement and use them to prevent your code from SQL Injection .同时阅读prepared statement并使用它们来防止您的代码被SQL Injection

Also always use password hashing mechanism while storing the password (if you used plain password).在存储密码时也始终使用password hashing mechanism (如果您使用普通密码)。

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

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