Total Geting Counts 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 there are 978 rows in the table as I see in PHPMyAdmin.

+3


source to share


2 answers


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

Try the following:



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

      

+7


source


Request

returns an object, you need to get 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();
}

      

-1


source







All Articles