SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2008
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to select a specific row from a MySQL result resource?

    My code generates a random number and then I want to select the row from a result set corrosponding to that number.

    I'm new to PHP & SQL and I can't find a way in the manual to select a specific row from a result resource, only mysql_fetch_array() which seems to just take the first row and then move some invisible pointer on to the next one.

    My code looks like this, but surely there is a more efficent and elegant way?

    Code:
    // Loop through results fetching the first 
    // row until I reach the <$rndTestNo>th row 
    
    for ($i = 1; $i <= $rndTestNo; $i++ )     
       $row = mysql_fetch_array($result);

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Code:
    SELECT * FROM table ORDER BY RAND() LIMIT 1
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2008
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks mate.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •