ballouta Posted October 20, 2008 Share Posted October 20, 2008 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 More sharing options...
Andy-H Posted October 20, 2008 Share Posted October 20, 2008 $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 More sharing options...
GKWelding Posted October 20, 2008 Share Posted October 20, 2008 <?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 More sharing options...
ballouta Posted October 20, 2008 Author Share Posted October 20, 2008 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 More sharing options...
Andy-H Posted October 20, 2008 Share Posted October 20, 2008 Yeh Link to comment https://forums.phpfreaks.com/topic/129200-query-rows-randomly/#findComment-669854 Share on other sites More sharing options...
Bendude14 Posted October 20, 2008 Share Posted October 20, 2008 try it Link to comment https://forums.phpfreaks.com/topic/129200-query-rows-randomly/#findComment-669855 Share on other sites More sharing options...
GKWelding Posted October 20, 2008 Share Posted October 20, 2008 yes it does, the srand() bit is only for versions of php older than 4.2 and that seeds the rand() function, however, in versions later than 4.2 you don't need to seed it and so just running rand() works fine. Link to comment https://forums.phpfreaks.com/topic/129200-query-rows-randomly/#findComment-669856 Share on other sites More sharing options...
ballouta Posted October 20, 2008 Author Share Posted October 20, 2008 great, thank you ALL Link to comment https://forums.phpfreaks.com/topic/129200-query-rows-randomly/#findComment-669857 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.