RE: Display just 1 record in a query

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

 



> -----Original Message-----
> From: Kim Madsen [mailto:php.net@xxxxxxx] 
> Sent: Tuesday, January 12, 2010 2:17 PM
> To: dealtek@xxxxxxxxx
> Cc: php-general@xxxxxxxxxxxxx
> Subject: Re:  Display just 1 record in a query
> 
> dealtek@xxxxxxxxx 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>&nbsp;</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>&nbsp;</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>&nbsp;</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.


-- 
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