简体   繁体   中英

How to get result from mysql when using Count(*) As

how can I get the result if this in php when my query is the following:

$query = "SELECT COUNT(*) AS call_count FROM `callsepaug` ";
        $query .= "WHERE start_time BETWEEN '" . $start_date . "' AND ";
        $query .= "'" . $end_date . "'";

It's the same as a simple query, you can get count(*) with call_count

$queryBuild = "SELECT COUNT(*) AS call_count FROM `callsepaug` ";
        $queryBuild .= "WHERE start_time BETWEEN '" . $start_date . "' AND ";
        $queryBuild .= "'" . $end_date . "'");

$query = mysqli_query($queryBuild);

$result =  mysqli_fetch_array($query);

echo $result['call_count'];

Or with object

$query = "SELECT COUNT(*) AS call_count FROM `callsepaug` ";
        $query .= "WHERE start_time BETWEEN '" . $start_date . "' AND ";
        $query .= "'" . $end_date . "'";

$result = mysqli_query($link, $query);

$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
echo $row["call_count"];

Try like this:

<?php
$query = "SELECT COUNT(*) AS call_count FROM `callsepaug` ";
        $query .= "WHERE start_time BETWEEN '" . $start_date . "' AND ";
        $query .= "'" . $end_date . "'";
$result = $mysqli->query($query);
$row_array=$result->fetch_array(MYSQLI_ASSOC);
print($row_array['TOTALFOUND']); 

Note: Mysql_* functions are deprecated. Avoid using these functions.

Incidentally, you can write that code this way...

$query = "
SELECT COUNT(*) call_count 
  FROM callsepaug
 WHERE start_time BETWEEN '$start_date' AND '$end_date';
 ";

Try this:

$query = mysql_query("SELECT COUNT(*) AS call_count FROM `callsepaug` ";
        $query .= "WHERE start_time BETWEEN '" . $start_date . "' AND ";
        $query .= "'" . $end_date . "'");

$result =  mysql_fetch_array($query);

echo $result[0];

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