Jump to content

Query rows randomly


ballouta

Recommended Posts

Hello

 

i have a table like this

 

ID    Book Name  Version    Year

==  ========    =====  ====

51  CSS              1          2008

52  HTML            2          2008

53  Book            1          2008

 

How can I query ALL books of version 1 AND year 2008, and every time a user open this page, it displays on the books out of the results?

I hope this is clear.

 

thank you very much

Link to comment
https://forums.phpfreaks.com/topic/129200-query-rows-randomly/
Share on other sites


$query = "SELECT * FROM table_name WHERE version='1' AND year='2008' ORDER BY ID DESC";
$result = mysql_query($query)or trigger_error("Query failed.");

while ($row = mysql_fetch_array($result)){

echo '<table><tr><td>etc... ' . $row['Book Name'] . '</td></tr></table>';

}

Link to comment
https://forums.phpfreaks.com/topic/129200-query-rows-randomly/#findComment-669844
Share on other sites

<?php

srand(); //only needed if PHP version is less than 4.2

$sql=mysql_query("SELECT * FROM books WHERE Version=1 and Year=2008 ORDER BY Rand() LIMIT 1");

WHILE ($row=mysql_fetch_array($sql)){

DISPLAY CODE;

}

?>

 

This method will generate a truely random book every page load...

Link to comment
https://forums.phpfreaks.com/topic/129200-query-rows-randomly/#findComment-669846
Share on other sites

Hello

i think i didn't write my question in a proper way.

 

I know how to make a query and display the results,

let's say a user has opene dthe books.php page, he should see one of the books that we query (version1 and year 2008), if he refreshes the page, opens it again, it should display another book of (year 2008 and version 1)

did u get what i mean?

thank you

 

Just saw GJWelding post"

so this line: $sql=mysql_query("SELECT * FROM books WHERE Version=1 and Year=2008 ORDER BY Rand() LIMIT 1");

does what i want?

Link to comment
https://forums.phpfreaks.com/topic/129200-query-rows-randomly/#findComment-669852
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.