SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot javierccs's Avatar
    Join Date
    Feb 2005
    Location
    Venezuela
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up Random function problem

    hello,

    im having trouble with the random function on a page, its selecting only the first register of the select
    PHP Code:
    $rsmotd mysql_query("select count(idmotd) motdct from motd where idmotd in (16,17,18)"$conn);
      
    $rowsmotd mysql_fetch_array($rsmotd);
      
    // Making the Random process using the count used before
      
    $radnmod rand(1,$rowsmotd["motdct"]);
      
    // Selecting the Random MOTD
      
    $rsmotd mysql_query("select * from motd where idmotd in (16,17,18)"$conn);
      
    $rowsmotd mysql_fetch_array($rsmotd); 
    i cant seem to find the problem but right now its just showing the motd wich id number is 16

    please, little help over here.....!!!!
    ...run Forrest....run!!

  2. #2
    SitePoint Addict Drinky's Avatar
    Join Date
    Jan 2001
    Location
    England
    Posts
    338
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_fetch_array() fetches the first row of the result set and moves the pointer to the next row, a subsequent call to mysql_fetch_array() will return the second row and so forth. Because you only ever call it once you will only ever get the first row of the result.

    I can't see where you use the variable $radnmod in this code slice, which you get from the rand() funtion. NOTE: that in php versions prior to 4.2.0 you need to seed the rand() function with srand() before calling rand() otherwise you will get the same random number sequence each time.

    For your code to work you would need to return the result set into an array and pick the index using your random number.

    PHP Code:
    // your code here
     
    while($aRow mysql_fetch_array($rsmotd)){
        
    $aResult[] = $aRow;
    }
     
    $aRandomRow $aResult[$radnmod]; 
    However this is somewhat inefficient (and will get worse as the number of rows in your table increase), you could use the MySQL SQL Limit statement to return your 1 random row.

    PHP Code:
    $rsmotd mysql_query("select count(idmotd) motdct from motd where idmotd in (16,17,18)"$conn); 
    $rowsmotd mysql_fetch_array($rsmotd); 
    // Making the Random process using the count used before 
    //$radnmod = rand(1,$rowsmotd["motdct"]); 
     
    $radnmod rand(0,$rowsmotd["motdct"]-1); 
     
    // Selecting the Random MOTD 
    // $rsmotd = mysql_query("select * from motd where idmotd in (16,17,18)", $conn); 
    // $rowsmotd = mysql_fetch_array($rsmotd);
     
    // Pick just one row from the table
    $rsmotd mysql_query("select * from motd where idmotd in (16,17,18) LIMIT $radnmod,1"$conn);
    $rowsmotd mysql_fetch_array($rsmotd); 
    Drinky

  3. #3
    SitePoint Zealot javierccs's Avatar
    Join Date
    Feb 2005
    Location
    Venezuela
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    sorry man, it doesnt work
    and it gives me this error
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/httpd/html/index.php on line 17

    this is line 17
    PHP Code:
    $rowsmotd mysql_fetch_array($rsmotd); 
    the problem starts because i have a 15 register table (motd) and i used the random function to selected one to show randomly,

    then i added 3 registers this morning, and i want to just select randomly these 3 new registers, this is the code i used for the 15 register table, it works ok
    PHP Code:
    $rsmotd mysql_query("select count(idmotd) motdct from motd "$conn);
      
    $rowsmotd mysql_fetch_array($rsmotd);
      
    // Making the Random process using the count used before
      
    $radnmod rand(1,$rowsmotd["motdct"]);
      
    // Selecting the Random MOTD
      
    $rsmotd mysql_query("select * from motd where idmotd='$radnmod'"$conn);
      
    $rowsmotd mysql_fetch_array($rsmotd); 
    ...run Forrest....run!!


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
  •