monkeybidz Posted March 13, 2010 Share Posted March 13, 2010 Is there a way I can limit the query results to 3? I cannot declare it in the actual sql query as it is shared by another. Here is a piece of the code. I need it to return only the first three rows. foreach ($qry_results as $_key => $_row){ $last_domain = $domain_name; extract($_row); Can it be done without specifying the LIMIT in the SQL QUERY? Link to comment https://forums.phpfreaks.com/topic/195148-limit-query-results/ Share on other sites More sharing options...
teamatomic Posted March 13, 2010 Share Posted March 13, 2010 see LIMIT HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/195148-limit-query-results/#findComment-1025724 Share on other sites More sharing options...
jacko_162 Posted March 13, 2010 Share Posted March 13, 2010 im still pretty new at PHP but this is how i limit my results; $results = mysql_query("SELECT * FROM table_name_here ORDER BY id DESC LIMIT 3"); editing the "LIMIT 3" to whatever you want your limits to be.. hope it helps? Link to comment https://forums.phpfreaks.com/topic/195148-limit-query-results/#findComment-1025729 Share on other sites More sharing options...
Instigate Posted March 13, 2010 Share Posted March 13, 2010 The easiest way is too have 2 separate query's and have different limits. There is no point in having one query when really you can have two and easily fix the problem. Link to comment https://forums.phpfreaks.com/topic/195148-limit-query-results/#findComment-1025731 Share on other sites More sharing options...
wildteen88 Posted March 13, 2010 Share Posted March 13, 2010 If you cannot modify the query for whatever reason you can use a counter $limit = 3; $i= 1; foreach ($qry_results as $_key => $_row) { if($i <= $limit) { $last_domain = $domain_name; extract($_row); // rest of your code here } else break; $i++ } Link to comment https://forums.phpfreaks.com/topic/195148-limit-query-results/#findComment-1025732 Share on other sites More sharing options...
monkeybidz Posted March 16, 2010 Author Share Posted March 16, 2010 Your on the right track wildteen88. I tried it, but the script still follows the original query and returns 10 rows. Link to comment https://forums.phpfreaks.com/topic/195148-limit-query-results/#findComment-1027136 Share on other sites More sharing options...
fr34k Posted March 16, 2010 Share Posted March 16, 2010 Your on the right track wildteen88. I tried it, but the script still follows the original query and returns 10 rows. A very basic example here: for($i = 0; $i <= 3; $i++) { if (isset($qry_results[$i])) print $qry_results[$i].'<br />'; else break; } Does that get you a little closer? Edit: Fixed my misspelled function name! Link to comment https://forums.phpfreaks.com/topic/195148-limit-query-results/#findComment-1027146 Share on other sites More sharing options...
monkeybidz Posted March 16, 2010 Author Share Posted March 16, 2010 I get three Array Line and 1 good result line. Array Array Array The Result Line That Is Good. Link to comment https://forums.phpfreaks.com/topic/195148-limit-query-results/#findComment-1027227 Share on other sites More sharing options...
ksugihara Posted March 16, 2010 Share Posted March 16, 2010 mysql results are returned as an array... So this table: Users Firstname Lastname ksugihara k sugihara fr34k php freak Would look like this: array ('Users' => array('ksugihara', 'fr34k'), 'Firstname' => array('k', 'php'), 'Lastname' => array('sugihara', 'freak')) So you would need to pull out those variables... but not like a normal array.. its weird. See mysql_fetch_assoc Link to comment https://forums.phpfreaks.com/topic/195148-limit-query-results/#findComment-1027232 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.