kryppienation Posted October 31, 2008 Share Posted October 31, 2008 Ok, This is prob a very simple thing to do but i don't know how. I am pulling info from a database and trying to make a random selection of the choices that are ok with my sql. In the example i have here, there are a total of 5 fish in the database that match my query. I need to now randomly select one of the results. I'm not sure how to do that, can anyone give me a hand? I left a copy of the code, and the output of the 5 fish in the database that were ok with the query. Thanks Everyone $selectfishsql = 'select * from fish where areacaught = 1 and rodused = 1'; DB::connect($DB_database); $selectfish = DB::query($selectfishsql, "select fish rod 1 area 1."); DB::close(); echo '<table border="1">'; while ($row = mysql_fetch_assoc($selectfish)) { $fishid = $row['fid']; $fishname = $row['fishname']; $areacaught = $row['areacaught']; $rodused = $row['rodused']; echo '<tr><td>' . $fishid . '</td>'; echo '<td>' . $fishname . '</td>'; echo '<td>' . $areacaught . '</td>'; echo '<td>' . $rodused . '</tr><p>'; } echo '</table>'; [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/130847-solved-how-to-pull-specific-rows/ Share on other sites More sharing options...
Daniel0 Posted October 31, 2008 Share Posted October 31, 2008 You can use a query like this: SELECT * FROM fish WHERE areacaught = 1 AND rodused = 1 LIMIT 1 ORDER BY RAND(); Link to comment https://forums.phpfreaks.com/topic/130847-solved-how-to-pull-specific-rows/#findComment-679130 Share on other sites More sharing options...
kryppienation Posted October 31, 2008 Author Share Posted October 31, 2008 i changed it to try that and it gave me an error. included is the code and error $selectfishsql = 'SELECT * FROM fish WHERE areacaught = 1 AND rodused = 1 LIMIT 1 ORDER BY RAND()'; DB::connect($DB_database); $selectfish = DB::query($selectfishsql, "select fish rod 1 area 1."); DB::close(); [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/130847-solved-how-to-pull-specific-rows/#findComment-679133 Share on other sites More sharing options...
Daniel0 Posted October 31, 2008 Share Posted October 31, 2008 Ooops... sorry. My bad. Switch the order and the limit around, i.e.: SELECT * FROM fish WHERE areacaught = 1 AND rodused = 1 ORDER BY RAND() LIMIT 1; PS: You can just copy and paste the errors, it's easier Link to comment https://forums.phpfreaks.com/topic/130847-solved-how-to-pull-specific-rows/#findComment-679137 Share on other sites More sharing options...
kryppienation Posted October 31, 2008 Author Share Posted October 31, 2008 Awesome!! Thanks a lot Take care now Link to comment https://forums.phpfreaks.com/topic/130847-solved-how-to-pull-specific-rows/#findComment-679138 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.