简体   繁体   中英

Geting total row count in MySQL

$rowCount = $conn->query('SELECT COUNT(*) FROM Users');
echo '<pre>'.print_r($rowCount,1).'</pre>';

returns:

mysqli_result Object
(
    [current_field] => 0
    [field_count] => 1
    [lengths] => 
    [num_rows] => 1
    [type] => 0
)

... although the table has 978 rows as I see in PHPMyAdmin.

You are using print_r to generate the number of rows in your query. Your query is only returning one row, which is the count of rows.

Try this:

$rowCount = $conn->query('SELECT COUNT(*) as rowNumber FROM Users');
$row = $rowCount->fetch_assoc();
echo $row['rowNumber'];

query returns an object, you need to fetch the result from that object

$sql = "SELECT COUNT(*) AS count FROM Users";

if ($res = $mysqli->query($sql)) {
    /* Fetch object array */
    while ($obj = $res->fetch_object()) {
        echo '<pre>'.print_r($obj->count,1).'</pre>';
    }
    $res->close();
}

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