Hi all,
I think there is a problem with database documentation.
It is not an error because it works but I think it is not clever.
Here’s the code
$query = $this->db->query('SELECT name, title, email FROM my_table');
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->email;
}
echo 'Total Results: ' . $query->num_rows();
The method ‘result()’ is called every time in the foreach. Even if it returns directly the array, it is not the best way to do it.
The foreach is not the best method anymore.
Why do not use a for instead because the key is not use.
I propose this.
$query = $this->db->query('SELECT name, title, email FROM my_table');
$aResult = $query->result();
$iNbRows = $query->num_rows();
for ( $i=0; $i<$iNbRows; $i++ ) {
$row = & $aResult[$i];
echo $row->title;
echo $row->name;
echo $row->email;
}
echo 'Total Results: ' . $iNbRows;
I do not agree with object by default too, but it is another problem.
Tell me if I am Wrong;
Thx.
