pepe pepe - 5 months ago 23
PHP Question

Discrepancy between CodeIgniter active record vs. MySQL queries

When I run this query using CodeIgniter's active records:

$user_name = 'mememe';
$test = $this->db->select('filename')
->where('user', $user_name)
->order_by('number','asc')
->limit(4)
->get('mytable')
->row('filename');
print_r($test); exit;


I get this result on my browser:

5f


But when I run the same query on my database manager:

SELECT `filename` FROM `mytable` WHERE `user` = 'mememe' ORDER BY `number` ASC LIMIT 4


I get this (which is actually correct and what I want):

5f
9f
10f
11f


Why is this happening?

Answer

From CI doc

row()

This function returns a single result row. If your query has more than one row, it returns only the first row. The result is returned as an object.

Jusr remove call to row method

 $test = $this->db->select('filename')
                    ->where('user', $user_name)
                    ->order_by('number','asc')
                    ->limit(4)
                    ->get('mytable');