gamerzfuse Posted May 12, 2009 Share Posted May 12, 2009 I have a query //Make first query to database $query = "SELECT * FROM vehicles"; $result = mysql_query($query) or die ("Couldn’t execute query."); //Set Variables $vehiclemodel = $vehicles->model->ViewValue; $query = "SELECT * FROM vehicles WHERE model='$vehiclemodel'"; $result = mysql_query($query) or die ("Couldn't execute query."); // Display results in a table echo "<tr><td colspan='5'></td></tr>"; while ($row = mysql_fetch_array($result)) { extract($row); //Table Contents $show_price = number_format($price); echo "<tr class='dataright'>\n <td colspan='5' class='dataleft'><a href='view.php?stock=$stock'> $year $model</a></td>\n <td>$odometer km </td>\n <td align='right'>$$show_price.00 </td>\n </tr>\n"; echo "<tr><td colspan='3'></td></tr>\n"; } I want to limit the results to only display # results. (ie: 10 results) Google searches for "limit query" etc haven't given me what I'm looking for. Any hints, links, etc would be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/157843-solved-limiting-results-of-query/ Share on other sites More sharing options...
JonnoTheDev Posted May 12, 2009 Share Posted May 12, 2009 $query = "SELECT * FROM vehicles LIMIT 10"; Quote Link to comment https://forums.phpfreaks.com/topic/157843-solved-limiting-results-of-query/#findComment-832538 Share on other sites More sharing options...
Stryves Posted May 12, 2009 Share Posted May 12, 2009 $query = "SELECT * FROM vehicles LIMIT 10"; Additionally, you can use the following to order by the somecolumn being a real column to select what area of first 10 you want. $query = "SELECT * FROM vehicles ORDER BY somecolumn LIMIT 10 "; Quote Link to comment https://forums.phpfreaks.com/topic/157843-solved-limiting-results-of-query/#findComment-832541 Share on other sites More sharing options...
gamerzfuse Posted May 12, 2009 Author Share Posted May 12, 2009 $query = "SELECT * FROM vehicles LIMIT 10"; Additionally, you can use the following to order by the somecolumn being a real column to select what area of first 10 you want. $query = "SELECT * FROM vehicles ORDER BY somecolumn LIMIT 10 "; Thanks loads guys, that was going to be my second question Stryves. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/157843-solved-limiting-results-of-query/#findComment-832543 Share on other sites More sharing options...
allworknoplay Posted May 12, 2009 Share Posted May 12, 2009 Depends on what you want to do with your application. LIMIT 10 will only show you the first 10 results. But if you want to then show the next 10 results, you have to provide it like so. LIMIT 1,10 = first 10 LIMIT 11,20 = next 10 Or you can looking into pagination to provide the full user experience... Quote Link to comment https://forums.phpfreaks.com/topic/157843-solved-limiting-results-of-query/#findComment-832547 Share on other sites More sharing options...
gamerzfuse Posted May 12, 2009 Author Share Posted May 12, 2009 Since you guys are so great at sorting, I'll throw another one out there: I have a list that is sorted based on the column header that is clicked (ie: click Price, sorts by Price) Is there anyway to have it secondarily sort? I have 400 items in the list and it lists like: Chevrolet 2008 Honda 2008 Chevrolet 2008 Know what I mean? EDIT: (I want it to sort secondarily by Make or Model regardless, just to keep things looking tidy.) Quote Link to comment https://forums.phpfreaks.com/topic/157843-solved-limiting-results-of-query/#findComment-832553 Share on other sites More sharing options...
Stryves Posted May 12, 2009 Share Posted May 12, 2009 $query = "SELECT * FROM vehicles ORDER BY somecolumn DESC, somecolumn2 LIMIT 10 "; If you want to go from highest to lowest, add DESC beside the column name, otherwise it's always ascending. Just put a comma between the 2 column names. Quote Link to comment https://forums.phpfreaks.com/topic/157843-solved-limiting-results-of-query/#findComment-832559 Share on other sites More sharing options...
gamerzfuse Posted May 12, 2009 Author Share Posted May 12, 2009 Just put a comma between the 2 column names. Yeah.. but see, the function I'm using gets the SORT from the URL. Meaning example.com/items.php?order=make&ordertype=ASC Can I automatically have a second one the defaults to secondary regardless? $sUrlParm = $this->UrlParm("type=" . mysql_real_escape_string($_GET['type']) . "&order=" . urlencode($fld->FldName) . "&ordertype=" . $fld->ReverseSort()); I'm not sure how I would enter commas into this variable? Quote Link to comment https://forums.phpfreaks.com/topic/157843-solved-limiting-results-of-query/#findComment-832561 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 You don't. You do that in the SQL. Quote Link to comment https://forums.phpfreaks.com/topic/157843-solved-limiting-results-of-query/#findComment-832562 Share on other sites More sharing options...
gamerzfuse Posted May 12, 2009 Author Share Posted May 12, 2009 You don't. You do that in the SQL. Yeah.. that stuff is way over my head. I guess I'll just give up on that one. Quote Link to comment https://forums.phpfreaks.com/topic/157843-solved-limiting-results-of-query/#findComment-832570 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.