<\/script>')

How to execute a num_rows () request in a COUNT request in a codegame?

It works:

        $sql = "SELECT id
                FROM `users`
                WHERE `account_status` = '" . $i . "'"; 
        $query = $this->db->query($sql);
        var_dump($query->num_rows());

But this is not so:

        $sql = "SELECT COUNT(*)
                FROM `users`
                WHERE `account_status` = '" . $i . "'"; 
        $query = $this->db->query($sql);
        var_dump($query->num_rows());

How to make num_rows in request COUNT (*)? And does this make the second way better performance?

+4
source share
9 answers

Executing COUNT(*)will give you a single line containing the number of lines, not the results themselves.

To access COUNT(*), you will need to do

$result = $query->row_array();
$count = $result['COUNT(*)'];

The second option works much better, since it does not need to return the data set to PHP, but instead just a count and, therefore, is much more optimized.

+13
source

In CI, it’s really simple, all you need is

$this->db->where('account_status', $i);
$num_rows = $this->db->count_all_results('users');
var_dump($num_rows); // prints the number of rows in table users with account status $i
+9
source
$query->num_rows()

, . . $query - , :

$query = $this->db->query('SELECT * FROM my_table');

echo $query->num_rows();
+7

num_rows COUNT() 1. GROUP BY, . count, SELECT COUNT(*) as myCount ..., (, ) "myCount".

+4

CI Docs ,

$this->db->where('account_status', $i); // OTHER CONDITIONS IF ANY
$this->db->from('account_status'); //TABLE NAME
echo $this->db->count_all_results();

- ,

echo $this->db->count_all_results('table_name'); // returns total_rows presented in the table
+1

1 , COUNT(). mysql_num_rows() $query .

ID, GROUP BY id .

, * . 100 , , 100, *. , * , , , , .

0

, , SELECT FOUND_ROWS()

0
    $list_data = $this->Estimate_items_model->get_details(array("estimate_id" => $id))->result();
    $result = array();
    $counter = 0;
    $templateProcessor->cloneRow('Title', count($list_data));
    foreach($list_data as $row) {
        $counter++;
        $templateProcessor->setValue('Title#'.$counter, $row->title);
        $templateProcessor->setValue('Description#'.$counter, $row->description);
        $type = $row->unit_type ? $row->unit_type : "";
        $templateProcessor->setValue('Quantity#'.$counter, to_decimal_format($row->quantity) . " " . $type);
        $templateProcessor->setValue('Rate#'.$counter, to_currency($row->rate, $row->currency_symbol));
        $templateProcessor->setValue('Total#'.$counter, to_currency($row->total, $row->currency_symbol));   
    }
0
$query = $this->db->get();
if ($query->num_rows() > 0) {
   echo 'have row';
} else {
   echo 'no row return from db';
}
0

All Articles