简体   繁体   中英

I want get number of records in my table , but it throw warning

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in

mysql_connect("localhost","root","");
mysql_select_db("db_usr");
$data=mysql_query("select * from tbl_usr");
$count=mysql_num_rows($data);
echo $count;

由于不建议使用mysql,请尝试使用mysqli。

Here's a quick way to echo the results if anyone is reading this page and missing that part:

$counter = mysql_query("SELECT COUNT(*) AS total FROM table");
$num = mysql_fetch_array($counter);
$count = $num["total"];
echo("$count");

mysql_query returns false(boolean) if there's an error.

before continuing you could check if the query went OK.

if(!$data) //error;

Try this out

$co=mysql_connect("localhost","root","");  
$con=mysql_select_db("db_usr",$co);  
$data=mysql_query("select * from tbl_usr");  
$count=mysql_num_rows($data);      
echo $count;

Try like this

$Conn = mysqli_connect("localhost","root","","db_usr");
$data = mysqli_query($Conn , "select * from tbl_usr");
$count = mysqli_num_rows($data);
echo $count;

Regards

Dhaval Pithva

Try mysql_error(). I think you provided a wrong db name or tbl name

 mysql_connect("localhost","root","");
 mysql_select_db("db_usr") or die(mysql_error());
 $data = mysql_query("select * from tbl_usr") or die(mysql_error());
 $count = mysql_num_rows($data);
 echo $count;

Try this

 $con=mysqli_connect("localhost","root","");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
 $result = mysqli_query($con, "select * from tbl_usr");
$rowcount=mysqli_num_rows($result);
echo $rowcount;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM