so I’m using the RAND() LIMIT 3, but I use it in a loop to get 3 random rows.
I need to use each column in a different place in my site. How can I grab the data without running a different query for each column?
The data I need shouldn’t be in order, so it’ll be for example:
$Title, will be the title of the page.
$price, will be the price of the service/product.
$description, will be the description of the service/product.
Clearly, they’re not close one to another in the php file.
Both answers sound logical, but I’m totally new to mysql and I can’t make it work.
This is what I have in my php file:
<?php
mysql_connect("localhost", "test", "test") or die (mysql_error ());
mysql_select_db("test") or die(mysql_error());
$strSQL = "SELECT name FROM UserId ORDER BY RAND() LIMIT 3";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo $row['name'] . "<br />";
}
mysql_close();
?>
What it does is return random ‘name’ column of 3 rows.
The table has ‘id’, ‘name’ and ‘Age’.
Your help is really appreciated!
Store them in
$_SESSIONonly if they are not already there, and always access them from$_SESSION.It still isn’t clear if you actually need this to persist across page loads. If you only need these rows on one single page and can get 3 different rows on other pages or subsequent page loads, there’s no need to store into
$_SESSIONand instead just store them into an array with:… and use the same
foreachpattern to iterate over$results.