我的sql最佳实践用php计数行 [英] my sql best practice with php for counting rows
本文介绍了我的sql最佳实践用php计数行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
1)计数记录:
//Connect to mysql server
$link = mysql_connect(HOST, USER, PASSWORD);
if(!$link) {
die('Could not connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DATABASE);
if(!$db) {
die("cannot use the database");
}
mysql_set_charset('charset=utf8',$link);
$query="SELECT `id`FROM `table` WHERE `abc`='123'";
$result=mysql_query($query);
$count= mysql_num_rows($result);
我使用它来计算记录。该表有500K条记录。计数记录的最佳做法是什么?
I am using this to count the record. The table has 500K records. What is the best practice to count the records?
2)我是初学者@ mysql和php。我在上面的脚本中错过了什么吗?我想我需要关闭mysql连接!
2) I am beginner @ mysql and php. Did I miss something in the above script? I think I need to close mysql connection in the end !
推荐答案
mysql有一个计数函数:
mysql has a count function:
http://dev.mysql .com / doc / refman / 5.1 / en / counting-rows.html
,以便您可以使用:
SELECT COUNT(*) FROM table WHERE abc=123
$ b b
或任何条件。
or whatever condition.
这篇关于我的sql最佳实践用php计数行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文