更有效的方式COUNT()MYSQL行通过PHP? [英] More efficient way to COUNT() MYSQL rows via PHP?

查看:102
本文介绍了更有效的方式COUNT()MYSQL行通过PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我目前在PHP中计数mysql行的方法:

  // GET total#of results and build $ start和$ end for main SQL语句
$ sql ='SELECT COUNT(*)FROM savedsearches WHERE user_id = \''。mysql_real_escape_string($ user_id)。'\'';
$ initial_query = mysql_query($ sql)或die(SQL error);
$ num_sql = mysql_fetch_array($ initial_query);
$ numrows = $ num_sql [0];
mysql_free_result($ initial_query);有没有更有效的方法来做这个?


=h2_lin>解决方案

否。



如果您需要更高的效率,请确保user_id列上有索引


Here is how I am currently counting mysql rows in PHP:

//GET total # of results and build $start and $end for main SQL statement
$sql= 'SELECT COUNT(*) FROM savedsearches WHERE user_id=\''.mysql_real_escape_string($user_id).'\'';
$initial_query = mysql_query($sql) or die("SQL error");
$num_sql = mysql_fetch_array($initial_query);
$numrows = $num_sql[0];
mysql_free_result($initial_query);

Is there a more efficient way to do this?

解决方案

No. If you want a count , you run the query you're already running.

If you need more efficiency, make sure there is an index on the user_id column

这篇关于更有效的方式COUNT()MYSQL行通过PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆