php使用mysql怎么查询数据库已经有多少条数据

如题所述

php使用mysql查询数据库已经有多少条数据使用sql的count函数实现。
示例代码如下:
<?php
//数据库连接
$conn=mysql_connect("localhost","root","root");
if(!$conn){
die("对不起,数据库连接失败! ").mysql_errno();
}
//选择数据库
mysql_select_db("testdb");
//sql语句
$sql="SELECT COUNT(*) AS count FROM user";
//执行sql
$query=mysql_query($sql,$conn);
//对结果进行判断
if(mysql_num_rows( $query)){
$rs=mysql_fetch_array($query);
//统计结果
$count=$rs[0];
}else{
$count=0;
}
echo $count;
?>
返回的$count就是当前数据库的记录条数。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-04-13
如果是客户端连接数据库的话,一条语句OK。select count(*) from tablename;
<?php
$conn=mysql_connect('localhost','root','password');//连接数据库
mysql_select_db('databasename',$conn);//选择要查询的数据库
$sql="select count(*) from tablename";//SQL查询语句
if($result=mysql_query($sql,$conn))
{
$aaa=mysql_fetch_row($result);
echo $aaa[0]; //输出表里面总记录数
}本回答被提问者采纳
相似回答