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

https://zhidao.baidu.com/question/2117924409592537627.html?qbl=relate_question_1&word=php%20%D4%B4%C2%EB%20%B2%E9%BF%B4%20%B1%ED%20%D5%BC%D3%C3%BF%D5%BC%E4

 

例代码如下:
<?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就是当前数据库的记录条数。

 

 

如果是客户端连接数据库的话,一条语句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]; //输出表里面总记录数
}

 

相关推荐

网友评论(0)