简体   繁体   中英

mysqli_num_rows is not working

What is the main prob with this code? while testing this code in sql in database sql 此代码在数据库sql中的sql中
it shows num as 3 But the code shown below, shows only 1 that should be 3.

$link = mysqli_connect("localhost", "root", "", "bot");
$count_winner=mysqli_query($link,"SELECT count(winner) AS num FROM 
subscribe_user WHERE winner ='yes' AND hitdate=CURDATE()")or 
 die(mysql_error());
$w=mysqli_num_rows($count_winner);
echo $w;

What you're seeing is expected. mysqli_num_rows gives you the number of rows in your result -- 1 . The num field in your result shows you the value of count(winner) -- 3 . These are different metrics.

But i want to print 3 as counted in DB SQL . How can i do that?

Instead of grabbing mysqli_num_rows , actually read the result:

$result = mysqli_query('...') or die('...');    
$w = $result->fetch_assoc()['num'];

Also, mysql_error() does not mix with the mysqli_ api. It should be mysqli_error() and passing the connection as its (single) argument.

Reference:

$link = mysqli_connect("localhost", "root", "", "bot");
$count_winner = mysqli_query($link,"SELECT count(winner) AS num FROM subscribe_user WHERE winner ='yes' AND hitdate=CURDATE()") or mysqli_error($link));
$row = mysqli_fetch_array($count_winner);
echo $row['num']; //Will output the count of number of winners

The mysqli_num_rows() function returns the number of rows in a result set.

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