Re: PHP Objects and SQL Results
| From: | Eric Lee | Date: | Fri, 12 Feb 2010 23:25:55 +0000 |
| Subject: | Re: PHP Objects and SQL Results | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-46879@lists.php.net to get a copy of this message | ||
On Sat, Feb 13, 2010 at 3:26 AM, Paul <devineman@msn.com> wrote:
> Hi all,
>
> I'm currently having a problem correctly formatting a table within a while
> loop. I'm using an object to store the results of a query, and using the
> while to iterate through it each row to produce the output:
>
> $query = "SELECT * FROM foo WHERE UserID = " .$uID . " ORDER BY bar";
> $result = mysql_query($query);
>
> while($obj = mysql_fetch_object($result))
> {
> $obj->bar;
> }
>
> To properly format the table, I need to check the value of bar in the next
> iteration of the object (but have to do it on the current one). Using an
> array, I would do:
>
> next($obj);
> if($obj["bar"] == "something")
> {
> //do things
> }
> prev($obj);
>
> Is there an equivalent to object? I've tried the above method, but nothing
> happens. I've also tried type casting it to an array, without success.
>
> Is there anyway to iterate through this?
>
Paul
Is this the one you want ?
$sql = 'select id, name from test';
$result = mysql_query($sql);
$rows = array();
$row = null;
while ($row = mysql_fetch_object($result))
{
$rows[] = $row;
}
reset($rows);
for ($i = 0, $c = sizeof($rows) - 1; $i < $c; $i++)
{
next($rows);
if (current($rows)->name)
{
// something to do
}
prev($rows);
echo current($rows)->id, ' ', current($rows)->name, "\n";
next($rows);
}
if (current($rows))
{
echo current($rows)->id, ' ', current($rows)->name, "\n";
}
Regards,
Eric,
> Thanks,
> Paul
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>