简体   繁体   中英

mysqli_num_rows is always 1

Can anyone tell me why this

$pages_query = mysqli_query($link, "SELECT COUNT(`id`) FROM `gallery`") or   
                            die(mysqli_error($link));
$row = mysqli_num_rows($pages_query);
echo $row;

echos 1 even though there are 7 rows in my table?

That's because mysqli_num_rows() will return rows count :) and your query will return only one row (with count field).

Use

$result = mysqli_fetch_array($pages_query, MYSQLI_NUM);

to get that count.

And it will since it always be 1 row (indicated exactly what you've selected, i'e COUNT).

You have two options:

$pages_query = mysqli_query($link, "SELECT COUNT(`id`) AS total_count FROM `gallery`") or   
                            die(mysqli_error($link));
$row = mysqli_fetch_array($pages_query);
echo $row['total_count'];

or:

$pages_query = mysqli_query($link, "SELECT * FROM `gallery`") or   
                            die(mysqli_error($link));
$row = mysqli_num_rows($pages_query);
echo $row;

-and first is much better, of cause.

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