RE: [PHP] Display just 1 record in a query
| From: | Daevid Vincent | Date: | Tue, 12 Jan 2010 23:00:49 +0000 |
| Subject: | RE: [PHP] Display just 1 record in a query | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-301047@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Kim Madsen [mailto:php.net@emax.dk]
> Sent: Tuesday, January 12, 2010 2:17 PM
> To: dealtek@gmail.com
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] Display just 1 record in a query
>
> dealtek@gmail.com wrote on 12/01/2010 22:52:
> > I did a query... then I display records like:
> >
> > <table>
> > <?php do { ?>
> > <tr>
> > <td><?php echo $row_cur['tid']; ?></td>
> > <td> </td>
> > </tr>
> > <?php } while ($row_cur = mysql_fetch_assoc($cur)); ?>
> > </table>
> >
> >
> > Q: but how I i just display a particular record with out
> the do / while
> > loop?
>
> Just use extract($row_cur); before the table starts. That
> would give you
> first row only
>
> Another approach could be to add " LIMIT 1" to the end of
> your SQL statement
>
> > like just the 2nd record only:
> >
> > i tried
> > <?php echo $row_cur['tid',2]; ?>
> > but this makes an error....
> >
> > or $row_cur('tid',2) --- hmmm what's the syntax?
>
> Getting only second row, but not the first? That would be
> using a count
> var and show only data if count == 2
>
>
> <table>
> <?php
> $count=0;
> do {
> $count++;
> if($count == 2) {
> echo '
> <tr>
> <td>'. $row_cur['tid'] .'</td>
> <td> </td>
> </tr>';
> }
> } while ($row_cur = mysql_fetch_assoc($cur));
> ?>
> </table>
>
>
> Another thing: drop the do and use this syntax instead, it's
> more readable:
>
> <table>
> <?php
> $count=0;
> while ($row_cur = mysql_fetch_assoc($cur)) {
> $count++;
> if($count == 2) {
> echo '
> <tr>
> <td>'. $row_cur['tid'] .'</td>
> <td> </td>
> </tr>';
> }
> }
> ?>
> </table>
Holy, Jesus, Marry and Joseph! You can't be serious with that?!
So you're going to loop over potentially hundreds or thousands of records
and only display one?
Wow. Speechless.
http://www.php.net/manual/en/function.mysql-data-seek.php
$result = mysql_query($query);
mysql_data_seek($result, 1); //rows start at 0, so second row is 1
$row = mysql_fetch_assoc($result);
echo $row['tid'];
But the real way I suggest is using the LIMIT portion of your SELECT
http://dev.mysql.com/doc/refman/5.0/en/select.html
Then you don't need to data_seek() as you would have pulled the exact row
you wanted.
If you wanted the second row, you would "LIMIT 1,1"
Also, if you just did a basic loop and stored all your results in a
multi-dimensional array, then you would just pull that element.
$mydata = array();
while ($row = mysql_fetch_assoc($result)) $mydata[] = $row;
Then you would just echo $mydata[1]['tid'];
Or what I like to do:
while ($row = mysql_fetch_assoc($result)) $mydata[$row['id']] = $row;
Assuming you were trying to pull a specific record $tid = ID:
echo $mydata[$tid];
ÐÆ5ÏÐ
Light travels faster than sound. This is why some people appear bright
until you hear them speak.