Web Analytics Made Easy -
StatCounter Selecting more than one thing from the DB - CodingForum

Announcement

Collapse
No announcement yet.

Selecting more than one thing from the DB

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Selecting more than one thing from the DB

    This is just an excerpt of code...

    Code:
    <?
    if(isset($_GET['area'])){
    $result = mysql_query("SELECT * FROM adventinf WHERE area='".$_GET['area']."' ", $db);
    if(!mysql_num_rows($result)) {
        echo "There is no such area.";
    }else{
    while($retval = mysql_fetch_array($result)){
    
    $id1 = $retval['id'];
    
    }
    }
    $id = rand($id1,$id1);
    ?>
    Ok.. so, in the database, there are say, 2 areas with the value of forest. now, click on that area, and it goes to this code, selecting all the rows with that area.. i want it to select the MINIMUM id number, and the MAXIMUM id number of the rows with the value of area that was inputed, then i want it random between the Min and Max.. how would i do that??

  • #2
    Use the min(), max(), and rand() functions.
    PHP Code:
    //This expression is from the MySQL manual
    //To obtain a random integer R in the range i <= R <= j, use the expression FLOOR(i + RAND() * (j – i)

    $query "SELECT min(id), max(id), FLOOR(min(id) + (RAND() * (max(id) - min(id))))
    WHERE area = '"
    .$_GET['area']."'"

    Comment

    Working...
    X