PHP Objects and SQL Results
PHP Objects and SQL Results
am 12.02.2010 20:26:01 von Paul
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?
Thanks,
Paul
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: PHP Objects and SQL Results
am 13.02.2010 00:25:55 von Eric Lee
--001636c5961dd2a92c047f6f96b8
Content-Type: text/plain; charset=UTF-8
On Sat, Feb 13, 2010 at 3:26 AM, Paul 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
>
>
--001636c5961dd2a92c047f6f96b8--
Re: PHP Objects and SQL Results
am 13.02.2010 00:46:34 von Paul Hollingworth
Thanks for the code Eric, it seems to loosely provide the functionality
that I'm after.
Just out of interest though, is there no other way to find the next
result row in an object apart from dumping it into an array?
Thanks,
Paul
Eric Lee wrote:
> On Sat, Feb 13, 2010 at 3:26 AM, Paul 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
>>
>>
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: PHP Objects and SQL Results
am 13.02.2010 01:05:51 von Karl DeSaulniers
--Apple-Mail-4--50457644
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed
Hi Paul,
Can't you just?
$query = "SELECT DISTINCT bar FROM foo WHERE UserID = " .$uID;
$result = mysql_query($query);
while($obj = mysql_fetch_assoc($result) {
$bar = $obj['bar'];
if ($bar == "something") {
//do this
}
}
I'm some what a beginner, so sorry if this wastes your time.
Karl
On Feb 12, 2010, at 1:26 PM, Paul 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?
>
> Thanks,
> Paul
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
Karl DeSaulniers
Design Drumm
http://designdrumm.com
--Apple-Mail-4--50457644--
Re: PHP Objects and SQL Results
am 13.02.2010 04:47:01 von Eric Lee
--000e0cd6ce029e5752047f733c5a
Content-Type: text/plain; charset=UTF-8
On Sat, Feb 13, 2010 at 7:46 AM, Paul Hollingworth wrote:
> Thanks for the code Eric, it seems to loosely provide the functionality
> that I'm after.
>
> Just out of interest though, is there no other way to find the next result
> row in an object apart from dumping it into an array?
>
>
Paul
Apologize !
I think no. The resource returned by mysql_query acts like a pointer (or
cursor on database side) that point to the current record
in result set. Before it is able advanced to the next the record must
retrieved first.
Might some design patterns be help for your situation.
And wait for some php pros that master in this area.
Regards,
Eric
Thanks,
> Paul
>
>
> Eric Lee wrote:
>
>> On Sat, Feb 13, 2010 at 3:26 AM, Paul 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
>>>
>>>
>>>
>>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--000e0cd6ce029e5752047f733c5a--
Re: PHP Objects and SQL Results
am 13.02.2010 09:54:30 von Moritz Fuchs
--0016e6469722386846047f7788e7
Content-Type: text/plain; charset=UTF-8
Hi,
Why don't you just try the following:
$query = "SELECT * FROM foo WHERE UserID = " .$uID . " ORDER BY bar";
$result = mysql_query($query);
//get the first row
$row = mysql_fetch_object($result);
//get the next row
while ($next = mysql_fetch_object($result)) {
//do something with row/next
//The next row is the current row in the next iteration
$row = $next;
}
mysql_free_result($result);
Regards
Moritz
On Fri, Feb 12, 2010 at 8:26 PM, Paul 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?
>
> Thanks,
> Paul
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--0016e6469722386846047f7788e7--
Re: PHP Objects and SQL Results
am 13.02.2010 21:54:30 von Richard Quadling
On 12 February 2010 23:46, Paul Hollingworth wrote:
> Thanks for the code Eric, it seems to loosely provide the functionality that
> I'm after.
>
> Just out of interest though, is there no other way to find the next result
> row in an object apart from dumping it into an array?
>
> Thanks,
> Paul
You can use mysql_result()
(http://docs.php.net/manual/en/function.mysql-result.php) to read a
specific row from the result set.
But you could also use the SQL WHERE or LIMIT clause to only retrieve
the specific row or rows you wanted.
--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php