Thursday, December 15, 2011

PHP: How to select a specific row from an SQL SELECT statement?

I used the following code to retrieve a bunch of rows from a database:





$sql="SELECT * FROM Homes WHERE FeatureHome=TRUE";


$result = mysql_query($sql);


$numrows = mysql_num_rows($result);


$random1=rand(0,$numrows);


$random2=rand(0,$numrows);


$random3=rand(0,$numrows);


$random4=rand(0,$numrows);





and now what I want to do is select 4 random rows from $result based on the 4 random numbers generated.





Also, is the "WHERE FeatureHome=TRUE" the correct way to check a binary variable in SQL?





Thanks in advance.|||For your first issue, use mysql_result.


$data1 = mysql_result($result,$random1);


$data2 = mysql_result($result,$random2);


$data3 = mysql_result($result,$random3);


$data4 = mysql_result($result,$random4);





As for checking a boolean, just use "WHERE FeatureHome". It will evaluate itself.

No comments:

Post a Comment