SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Berlin
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using list with mysql_fetch_assoc

    I'm working on a random query and would like to use the list() method.
    Unfortunately list() does not work with mysql_fetch_assoc.

    What could I do to have this funktion work with something like

    PHP Code:
    list($s_fid) = mysql_fetch_assoc($f_query); 
    The working code:
    PHP Code:
    $f_query mysql_query("select fid from database WHERE xyz=0"$dbi);
    $rows=mysql_num_rows($f_query);
    srand((double)microtime()*1000000);
    $id=rand(0,$rows-1);
    mysql_data_seek($f_query,$id);
    $row=mysql_fetch_assoc($f_query);
    echo 
    "$row[fid]"

    Regards
    Fl÷zen

  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)
    There are no funky PHP shenanigans needed, just one MySQL one.
    PHP Code:
    <?php
    $sSQL 
    "SELECT fid FROM table WHERE xyz = 0 ORDER BY RAND() LIMIT 1;";
    $rResult mysqli_query($rConnection$sSQL);
    $aRow mysqli_fetch_assoc($rResult);
    echo 
    $aRow['fid'];
    ?>
    @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 Addict
    Join Date
    Feb 2003
    Location
    Berlin
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is a random generated result with RAND() in MySQL faster than with PHP?

    I have heard, that RAND() stresses the DB a lot, but I don't kown under which condition.
    My query would be within approx. 10.000 data-set-entries. The table will be about 1,5MB large.

  4. #4
    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)
    I think this only applies to very large databases, try the query and run some benchmarks; I would put money on it that it will be much more efficient that manipulating a complete result set PHP side.

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


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
  •