CodeIgniter Forums
mysql_fetch_row() in CodeIgniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: mysql_fetch_row() in CodeIgniter (/showthread.php?tid=25467)

Pages: 1 2


mysql_fetch_row() in CodeIgniter - El Forum - 12-12-2009

[eluser]punchi[/eluser]
Code:
<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($result);

echo $row[0]; // 42
echo $row[1]; // the email value
?>

Source: http://cl.php.net/manual/en/function.mysql-fetch-row.php

----

I have a code like this (just an example):

Code:
<?
foreach ($query->result() as $row)
{

echo $row->NAME;
echo $row->AGE;
}
?>

But because I change sometimes the SQL for other pages, I NEED something like the fetch_row, and make possible something like this:

Code:
<?
foreach ($query->result() as $row)
{

echo $row[0];
echo $row[2];
}
?>

In the User Guide, I didn't find anything... any trick? it's possible? its maybe a Feature Requests? =O

thanks people! =D


mysql_fetch_row() in CodeIgniter - El Forum - 12-12-2009

[eluser]skunkbad[/eluser]
how about result_array() ? I know you are looking for numberic indices, so perhaps you can extend the method?


mysql_fetch_row() in CodeIgniter - El Forum - 12-13-2009

[eluser]umefarooq[/eluser]
here is the user guide link for your problem.

http://ellislab.com/codeigniter/user-guide/database/results.html


mysql_fetch_row() in CodeIgniter - El Forum - 12-13-2009

[eluser]punchi[/eluser]
[quote author="skunkbad" date="1260702146"]how about result_array() ? I know you are looking for numberic indices, so perhaps you can extend the method?[/quote]

This code:

Code:
<?
foreach ($query->result_array() as $row)
{

echo $row[0]; //tried $row['0'];
echo $row[2]; //tried $row['2'];
}
?>

Doesn't work.

What you mean with extend the method? Modify the core of CodeIgniter?

[quote author="umefarooq" date="1260709217"]here is the user guide link for your problem.

http://ellislab.com/codeigniter/user-guide/database/results.html[/quote]

My friend, its the SAME link I provided (read my post) where I didn't find any solution.


mysql_fetch_row() in CodeIgniter - El Forum - 12-13-2009

[eluser]skunkbad[/eluser]
[quote author="umefarooq" date="1260709217"]here is the user guide link for your problem.

http://ellislab.com/codeigniter/user-guide/database/results.html[/quote]

The OP's problem is, there is no result method that will produce an array with numeric indices. In plain PHP, mysql_fetch_array returns BOTH numeric and associative indices, but CI's result_array() only returns associative indices.


mysql_fetch_row() in CodeIgniter - El Forum - 12-13-2009

[eluser]skunkbad[/eluser]
I was looking at the manual, and you might be able to use:

http://ellislab.com/codeigniter/user-guide/database/call_function.html


mysql_fetch_row() in CodeIgniter - El Forum - 12-13-2009

[eluser]jedd[/eluser]
[quote author="punchi" date="1260745667"]
Code:
foreach ($query->result_array() as $row)
{
    echo $row[0]; //tried $row['0'];
    echo $row[2]; //tried $row['2'];
}
[/quote]

Either a) reassess why you want to be able to this (as you might be happier to just use the normal array walking functions of PHP - like everyone else does), or b) in the model you can massage your return data to look like this (I really do prefer option (a))).

For example:
Code:
$rows = $query->result_array();
$retdata = array();
foreach ($rows as $row)
    $retdata[] = $row;
return $retdata;



mysql_fetch_row() in CodeIgniter - El Forum - 12-13-2009

[eluser]punchi[/eluser]
@skunkbad

Thanks, I didn't saw that page =O... it works! =D however its too slow =/ ... the code its like this

(NOTE: I must be done with mysql_query() and NOT with $this->db->query(), or it won't work)

MODEL:
Code:
$sql = 'SELECT ID_PLAN, PL_NAME FROM plan';

$query = mysql_query($sql);

    while($row = $this->db->call_function('fetch_row', $query)) {
        $return[] = $row;
    }
        
return $return;

The controller just pass the upper return to a "query" data

VIEW:
Code:
foreach ($query as $row)
{
    echo $row[0]." - ".$row[1]."<br>";
}

It works fine but again, too slow =/

-------

@jedd

Im happy to just use the normal array like everyone does, but the "normal array" is NOT the solution for every problem. I want to show the information as wrote in SQL, so, if my sql is SELECT X, Y, Z, i want to see X, Y, Z, and when I change to SELECT Y, Z, X, see the same order.

And thanks jedd, but your solution didn't work, or I made a mistake...

MODEL:
Code:
$sql = 'SELECT ID_PLAN, PL_NAME FROM plan';

$temp = $this->db->query($sql);

$rows = $temp->result_array();

$retdata = array();

    foreach ($rows as $row){
        $retdata[] = $row;
    }

return $retdata;

VIEW:
Code:
print_r(array_count_values($query));

/*
--&gt; ERROR: Can only count STRING and INTEGER values!
*/

The error its enough to tell me that, there's no integer or string values (before have tried with $query[0], $query["PL_NAME"] and $query->PL_NAME too, all errors)


...Still can't get a good solution :down:


mysql_fetch_row() in CodeIgniter - El Forum - 12-13-2009

[eluser]punchi[/eluser]
My solution:

Change

Code:
while($row = $this->db->call_function('fetch_row', $query)) {
    $return[] = $row;
}

for

Code:
while($row = mysql_fetch_row($query)) {
    $return[] = $row;
}

The page speed up a 350% (from 0.0035 to 0.0010). The differences between foreach ($query2 as $row) and foreach ($query->result() as $row) are insignificant.

And now, between the "almost-always-used"
Code:
$sql = 'SELECT ID_PLAN, PL_NAME plan';
$query = $this->db->query($sql);

and
Code:
$sql = 'SELECT ID_PLAN, PL_NAME plan';
$query = mysql_query($sql);

        while($row = mysql_fetch_row($query)) {
                $return[] = $row;
        }

return $return;

The last one, its 170% more efficient. Another reason Jedd ;-)

What I don't know, if working with CodeIgnite and PHP native functions, have some incompatibilities or is inappropriate...

Thanks! =)


mysql_fetch_row() in CodeIgniter - El Forum - 12-13-2009

[eluser]jedd[/eluser]
[quote author="punchi" date="1260755489"]
Im happy to just use the normal array like everyone does, but the "normal array" is NOT the solution for every problem. I want to show the information as wrote in SQL, so, if my sql is SELECT X, Y, Z, i want to see X, Y, Z, and when I change to SELECT Y, Z, X, see the same order.
[/quote]

Hang on - you're saying that you want to ignore the ordering or your SQL function? Why not just get the ordering done properly in the first place?

Quote:VIEW:
Code:
print_r(array_count_values($query));
/*
--&gt; ERROR: Can only count STRING and INTEGER values!
*/

I'm assuming that $query in your controller is taking the $retdata from the model? You didn't show the code - and I don't like assumptions.

In any case, print_r() of $query here would be far more instructive, I suspect.

What does sizeof() report?

[quote author="punchi" date="1260759436"]
The page speed up a 350% (from 0.0035 to 0.0010).
[/quote]

How big is your sampling set, and under what conditions? This differential seems pretty trivial at a glance.


Quote:What I don't know, if working with CodeIgnite and PHP native functions, have some incompatibilities or is inappropriate...

What kind of incompatibilities are you thinking of here? I think they'd probably have noticed if such things existed by now. Wink