Re: mysql_fetch_object and modulo %

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2008-11-19 at 10:12 -0500, Andrew Ballard wrote:
> On Wed, Nov 19, 2008 at 9:32 AM, Craige Leeder <cleeder@xxxxxxxxx> wrote:
> > Alain Roger wrote:
> >>
> >> Hi,
> >>
> >> how can i do to test if the row number (record number) sent by
> >> mysql_fetch_object is odd or even ?
> >> i mean that:
> >> while ($row = mysql_fetch_object($result))
> >>    {
> >>     if($row%2 == 1)
> >>     {
> >>      ...
> >>     }
> >>    }
> >>
> >> the $row doesn't return a value... like 1, 2 or 6 for example...
> >> thx.
> >> F
> >>
> >>
> >
> > That's because $row is an object. Return your row uid (unique identifier;
> > primary key w/ auto increment) and do:
> >
> > while ( $row = mysql_fetch_object($result) )
> >   {
> >    if( $row->UID % 2 == 1 )
> >    {
> >     ...
> >    }
> >   }
> >
> >
> >
> > replacing UID with whatever you called the field in the database.
> >
> > Hope this helps,
> > - Craige
> >
> 
> That only works if the query returns rows with contiguous IDs, which
> is not always the case. If rows get deleted there will be gaps in an
> autonumber field. Even if no rows are deleted, it is quite common for
> a query to return rows with non-adjacent ID numbers because of the
> query conditions. It's better to use the counter approach that Nathan
> demonstrated.

It also doesn't work if the ID is anything but a number... such as an
MD5 hash. Nathan posted the correct solution.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux