ZF-9796: Zend_Db_Select Limit Sets rows to PHP_INT_MAX

Description

If you do something like the below:

    require_once 'Zend/Db/Select.php';
        require_once 'Zend/Db/Adapter/Mysqli.php';

    $select = new Zend_Db_Select(new Zend_Db_Adapter_Mysqli(array('dbname' => 'foo','password' =>'foo','username' =>'foo')));
    $select->limit(0, 10);

    echo $select;

it returns the following query:

LIMIT 2147483647 OFFSET 10

notice how the 0 was changed to PHP_INT_MAX value? Someone in the IRC channel confirmed this is a bug because the validation in Zend_Db_Select calls !empty(0)

Comments

Obviously, passing 0 as the limit does not make much sense, but the Zend_Db_Select::limit() function is explicitly setting the value to PHP_INT_MAX before it calls the adapter specific version of the limit() function.

So the exceptions that would otherwise be thrown by each specific adapter, do not get seen!

There is also possibly another logic problem in the limit() function. If you were to remove the setting of the limit count to PHP_INT_MAX, then the adaptor limit() function would not get called at all!

Even more bizarre, mysql will actually take limit 0 and offset 10 and give you exactly what you asked for... nothing - should Zend_Db be treating this as an error if mysql doesn't?

I am thinking that Zend_Db_Select::limit() should simply hand the values over to the adaptors and let them decide if it is valid or not.

I have just hit this issue. The behavior with zero explicitly set is totally unexpected. Ye, If i put there zero, give me zero rows and NOT all. I just wanted to test part of a script and cause of this i run it sharp.