Jump to content

Limit Query Results


monkeybidz

Recommended Posts

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

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

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

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

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

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.