简体   繁体   中英

How to retrieve result of COUNT(*) statement through PHP

How would I retrieve the result of the following COUNT(*) MYSQL statement using PHP?

MYSQL

$sql= mysql_query ("
                    select count(*)
                    from orders o
                    WHERE order_status IN
                    ('Printed Order', 'Charged Order', 'Exchanged Order', 'Refunded Order', 'Cancelled Order')
                    GROUP BY FIELD
                    (order_status,'Printed Order', 'Charged Order', 'Exchanged Order', 'Refunded Order', 'Cancelled Order')
                  ");

Result of the above query

count(*)
226
19130
593
2603
2892

I've tried the following code to retrieve each row in sequence -

PHP

$row = mysql_fetch_row($sql);
echo $row[0];
echo $row[1];
echo $row[2];
echo $row[3];
echo $row[4];

Which produces the following error : Notice: Undefined offset: 1 ...

I know I'm missing something really simple, but would somebody please show me the correct way to call the result of the COUNT(*) query.

Your query returns 5 rows. You have to loop over them and display the column:

while($row = mysql_fetch_row($sql)){
    echo $row[0];
}

See also the documentation

It also make sense using an alias for the count.

$sql= mysql_query = "
                    select count(*) as countValue
                    from orders o
                    WHERE order_status IN
                    ('Printed Order', 'Charged Order', 'Exchanged Order', 'Refunded Order', 'Cancelled Order')
                    GROUP BY FIELD
                    (order_status,'Printed Order', 'Charged Order', 'Exchanged Order', 'Refunded Order', 'Cancelled Order')
                  ";

while($row = mysql_fetch_row($sql)){
    echo $row["countValue"];
}

Warning: The mysql_ extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

you need to add loop to get all the data and use mysqli instead of mysql.

something like this

 $sql= "select count(*) as total from orders o WHERE order_status IN
       ('Printed Order', 'Charged Order', 'Exchanged Order', 'Refunded Order', 'Cancelled Order') GROUP BY FIELD (order_status,'Printed Order', 'Charged Order', 'Exchanged Order', 'Refunded Order', 'Cancelled Order')"

$result = mysqli_query($con, $sql);

while($row = mysqli_fetch_array($result)) {
   echo $row['total'];
   echo "<br>";
}

you can also use the same approach with mysql instead of mysqli but mysqli would be better for future use.

$result = mysql_query($sql);

while($row = mysql_fetch_row($sql)){
   echo $row['total'];
   echo "<br>";
}
$sql= mysql_query ("
                    select count(*) as nbre
                    from orders o
                    WHERE order_status IN
                    ('Printed Order', 'Charged Order', 'Exchanged Order', 'Refunded Order', 'Cancelled Order')
                    GROUP BY FIELD
                    (order_status,'Printed Order', 'Charged Order', 'Exchanged Order', 'Refunded Order', 'Cancelled Order')
                  ");


echo $row[0]['nbre'];

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