Fetching multiple random rows from a database
Tuesday, May 5, 2009
As a follow up to my earlier article about fetching a single random row from MySQL I will tell you today, how you can fetch multiple random rows from a table without any hassle.Read the entire article on devzone.zend.com
Comments to this article
Leave a comment
Please note that your email address will not be shown, it is only used to fetch your avatar image from gravatar.com and for notifications.


<?php
//CONTAIN THE CODE THAT SHOWING THE ROOMS AVAILABALE OR NOT
include "configopen.php"; //INCLUDE FILE CONFIGOPEN.PHP TO CONNECT TO DATABASE
// $sub=$_POST[To_Date]-$_POST[From_Date];
$sql= " SELECT Hotelid,RoomType,Image,HotelName,HotelAddr,Discription,HotelType,HotelPackage,Rooms_onrent,
TotalRooms,count(Hotelid),count(TotalRooms)
FROM Hotelinfo
WHERE TotalRooms > '0' AND Hotelinfo.Hotelid=Hotelinfo.Hotelid
ORDER BY TotalRooms,RoomPrice_Range ASC";
/* $sql=" SELECT TotalRooms,Hotelid
FROM Hotelinfo
WHERE Hotelid = (SELECT FLOOR( Hotelid * RAND()) FROM Hotelinfo ) ORDER BY Hotelid,TotalRooms ";
*/
//AND Customerinfo.Customerid=Customerinfo.Hotelid AND Hotelinfo.Customerid=Hotelinfo.Hotelid
$res=mysql_query($sql);
print(mysql_error());
global $i;
if($disp=mysql_fetch_array($res))
{
$count=$disp['count(Hotelid)'];
// for($i=0;$i< $count;$i++)
for($i=$disp[10];$i>0;$i--)
{
printf("Hotel Number=%d",$i);
?>
<table width="582" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="5"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="24%"><div align="left"><span class="style1">Hotel ID </span>
<input name="textfield3" type="text" id="textfield3" value="<? echo $disp[Hotelid]; ?>" size="10" readonly="readonly"/>
</div></td>
<td width="38%"><div align="left"><span class="style1">Hotel Name </span>
<input name="textfield4" type="text" id="textfield4" value="<? echo $disp[HotelName]; ?>" size="22" readonly="readonly"/>
</div></td>
<td width="38%"><div align="left"><span class="style1">Total Rooms </span>
<input name="textfield5" type="text" id="textfield5" value="<? echo $disp[TotalRooms]; ?>" size="20" readonly="readonly"/>
</div></td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="5"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="150"><span class="style1">Image</span></td>
<td width="13"> </td>
<td width="285" class="style1">Description</td>
</tr>
<tr>
<td><div align="center"><img src="<? echo $disp[Image]; ?>" width="150" height="150"/> </div></td>
<td> </td>
<td><form id="form1" name="form1" method="post" action="">
<textarea name="textarea" id="textarea" cols="55" rows="8"><? echo $disp[Discription];?></textarea>
</form> </td>
</tr>
</table></td>
</tr>
</table>
<? // } //end of inner for loop
}//end of outer for loop
echo "<script lanagauage='javascript' alert('Rooms Available ..');</script>";
}//closing of if
include "closedb.php"; //INCLUDE CLODEDB.PHP TO CLODE DATABSE CONNECTION
?>
//how shall i fetch random rows from my hotelinfo table ,i want those rows to be display that havin total number of rooms greater than 0
Regards ,
pradeep