dongzuozhu66776 2014-06-30 21:01
浏览 35
已采纳

计算数据库中页面的最快方法

I'm creating a small website with PHP and MySql, and i need a fast way to count all the pages stored in the database. I know 3 ways to do this, but since I'm not much of an expert in PHP, MySql i have no idea which is faster or better.

The first way will be to use the PHP's mysql_num_rows function.

$query = mysqli_query($dbcon, "SELECT * FROM `pages`");
$count = mysqli_num_rows($query);
echo $count;

The second way will be to use the MySql command SELECT COUNT(*)

$query = mysqli_fetch_assoc(mysqli_query($dbcon, "SELECT COUNT(*) FROM `pages`"));
echo $query["COUNT(*)"];

And the third way will be to create a database table row to hold the total number of pages, which will increase/decrease every time i create or delete a page.

I'm not sure which one to use, I've tested all, and they work just about the same with about 200 pages.

  • 写回答

1条回答 默认 最新

  • doukong6031 2014-06-30 21:04
    关注

    Select count(*)

    That sounds like the better way for a MyIsam storage engine at least, which stores that value and update it when there is any INSERT/DELETE/TRUNCATE. So there is no calculation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?