Search code examples
phpmysqlselectprestashoplimit

PHP with mysql SELECT LIMIT


I have a request (mysql) in PHP who gives me many results and it works like a charm. Here is an example of a request who gives me 115 results :

public static function getInfo($start, $limit) {
 $sql = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(
  SELECT blablabla FROM blabla ORDER BY bla LIMIT $start,$limit
 );
 return $sql;
}

In my php file, i've got :

$rq = getInfo (0, 0);
$count_rq = (int)count($rq);
for ($i = 0; $i < $count_rq; ++$i) {
 .... do my things ....
}

Here is the problem : in this configuration, it shows only results 1 to 42...

When i do some modifications/tests, it shows strange results :

Modification => for ($i = 42; $i < $count_rq; ++$i) {.......}
I got 0 result.

Modification => for ($i = 43; $i < $count_rq; ++$i) {.......}
I got results 43 from 115 !..

Modification => for ($i = 60; $i < $count_rq; ++$i) {.......}
I got results 60 from 115 !..

I've tried to modify the "LIMIT" but i don't understand. Here is the result when i do modifications/test :

(i'm using for ($i = 0; $i < $count_rq; ++$i) {.......})
 Modification => $rq = getInfo (0, 20);
     I got results 1 to 20.
 Modification => $rq = getInfo (0, 50);
     I got results 1 to 42.
 Modification => $rq = getInfo (10, 0);
     I got results 1 to 42.
 Modification => $rq = getInfo (10, 30);
     I got results 11 to 40.

I don't know how I can show all of my results? I want to have a code who can show 115 results when request got 115 results, or 10 results when request got 10 results. How can I do that? Why am I limited to first 42 results?


Solution

    • You use limit in your request and check that MySQL SELECT is retrieving good results.
    • Then, why do you start for loop in any other thing than 0??
    • If your request is getting 115 rows you should use this sentence to iterate through all of them:

      for ($i = 0; $i < $count_rq; ++$i)

    Good luck.