PDA

View Full Version : Selecting more than one thing from the DB


king2k5
09-26-2006, 11:22 PM
This is just an excerpt of 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??

Fumigator
09-26-2006, 11:44 PM
Use the min(), max(), and rand() (http://dev.mysql.com/doc/refman/4.1/en/mathematical-functions.html) functions.

//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']."'";