How to use MySQL Found_Rows() in PHP?

◇◆丶佛笑我妖孽 提交于 2019-11-26 16:58:23

问题


I try to avoid doing Count() because of performance issue. (i.e. SELECT COUNT() FROM Users)

If I run the followings in phpMyAdmin, it is ok:

  SELECT SQL_CALC_FOUND_ROWS * FROM Users;
  SELECT FOUND_ROWS();

It will return # of rows. i.e. # of Users.

However, if I run in in PHP, I cannot do this:

  $query = 'SELECT SQL_CALC_FOUND_ROWS * FROM Users;
    SELECT FOUND_ROWS(); ';
  mysql_query($query);

It seems like PHP doesn't like to have two queries passing in. So, how can I do that?


回答1:


SQL_CALC_FOUND_ROWS is only useful if you're using a LIMIT clause, but still want to know how many rows would've been found without the LIMIT.

Think of how this works:

SELECT SQL_CALC_FOUND_ROWS * FROM Users;

You're forcing the database to retrieve/parse ALL the data in the table, and then you throw it away. Even if you aren't going to retrieve any of the rows, the DB server will still start pulling actual data from the disk on the assumption that you will want that data.

In human terms, you bought the entire contents of the super grocery store, but threw away everything except the pack of gum from the stand by the cashier.

Whereas, doing:

SELECT count(*) FROM users;

lets the DB engine know that while you want to know how many rows there are, you couldn't care less about the actual data. On most any intelligent DBMS, the engine can retrieve this count from the table's metadata, or a simple run through the table's primary key index, without ever touching the on-disk row data.




回答2:


Its two queries:

$query = 'SELECT SQL_CALC_FOUND_ROWS * FROM Users';
mysql_query($query);
$query = 'SELECT FOUND_ROWS()';
mysql_query($query);

PHP can only issue a single query per mysql_query call




回答3:


It's a common misconception, that SQL_CALC_FOUND_ROWS performs better than COUNT(). See this comparison from Percona guys: http://www.mysqlperformanceblog.com/2007/08/28/to-sql_calc_found_rows-or-not-to-sql_calc_found_rows/

To answer you question: Only one query is allowed per one mysql_query call, as described in manual: mysql_query() sends a unique query (multiple queries are not supported)

Multiple queries are supported when using ext/mysqli as your MySQL extension:

http://www.php.net/manual/en/mysqli.multi-query.php




回答4:


Use 'union' and empty columns:

$sql="(select sql_calc_found_rows tb.*, tb1.title
      from orders tb
           left join goods tb1 on tb.goods_id=tb1.id
      where {$where}
      order by created desc
      limit {$offset}, {$page_size})
      union
      (select found_rows(), '', '', '', '', '', '', '', '', '')
     ";
$rs=$db->query($sql)->result_array();
$total=array_pop($rs);
$total=$total['id'];



回答5:


Only this code works for me so i want to share it for you.

$Result=mysqli_query($i_link,"SELECT SQL_CALC_FOUND_ROWS  id From users LIMIT 10"); 
$NORResult=mysqli_query($i_link,"Select FOUND_ROWS()"); 
$NORRow=mysqli_fetch_array($NORResult); 
$NOR=$NORRow["FOUND_ROWS()"]; 
echo $NOR;



回答6:


This is an easy way & works for me :

$query = "
SELECT SQL_CALC_FOUND_ROWS * 
FROM tb1
LIMIT 5";

$result = mysqli_query($link, $query);

$query = "SELECT FOUND_ROWS() AS count";
$result2 = mysqli_query($link, $query);

$row = mysqli_fetch_array($result2);

echo $row['count'];



回答7:


Do you really think that selecting ALL rows from tables is faster than counting them?
Myisam stores a number of records in table's metadata, so SELECT COUNT(*) FROM table don't have to access data.



来源:https://stackoverflow.com/questions/3453809/how-to-use-mysql-found-rows-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!