I have a native PHP function issue when I convert plain PHP code into CodeIgniter. Do have any idea or alternate solution about this?
Plain PHP code
$aColumns = array( 'id', 'name', 'first_name' );
$sTable = "ajax";
$sQuery = "SELECT SQL_CALC_FOUND_ROWS
".str_replace(" , ", " ", implode(", ", $aColumns))."FROM $sTable
$rResult = mysql_query( $sQuery );
while ( $aRow = mysql_fetch_assoc( $rResult ) )
{
print_r($aRow);
}
Perfect Result Output
Array
(
[id] => 1
[name] => kane
[first_name] => kane
)
Array
(
[id] => 2
[name] => kane
[first_name] => kane
)
Array
(
[id] => 3
[name] => kane
[first_name] => kane
)
Codeigniter CODE
$aColumns = array( 'id', 'name', 'first_name' );
$sTable = "ajax";
$sQuery = "SELECT SQL_CALC_FOUND_ROWS
".str_replace(" , ", " ", implode(", ", $aColumns))."FROM $sTable
$rResult = $this->db->query($sQuery);
while ( $aRow = $rResult->row_array() )
{
print_r($aRow);
}
Infinite Result Output
Array
(
[id] => 1
[name] => kane
[first_name] => kane
)
Array
(
[id] => 1
[name] => kane
[first_name] => kane
)
Thats because the use of
$result->row_array()is wrong in what you’re trying to accomplish.The correct way, to loop over the results and getting the result as an array is using the
$result->result_array()method instead. Like so:Take a look at the documentation for generating query results in CI.