slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.96k stars 1.95k forks source link

Division by zero error on query execution #928

Closed ekoome closed 9 years ago

ekoome commented 9 years ago

I'm getting the following Slim error when trying to execute Idiorm query.

[2014-11-06 14:37:58] SlimMonoLogger.ERROR: exception 'ErrorException' with message 'Division by zer o' in /var/www/html/api/v1/index.php:20 Stack trace: #0 /var/www/html/api/v1/index.php(20): Slim\Sli m::handleErrors(2, 'Division by zer...', '/var/www/html/a...', 20, Array) #1 [internal function]: {c losure}() #2 /var/www/html/api/v1/vendor/slim/slim/Slim/Route.php(462): call_user_func_array(Object( Closure), Array) #3 /var/www/html/api/v1/vendor/slim/slim/Slim/Slim.php(1326): Slim\Route->dispatch( ) #4 /var/www/html/api/v1/vendor/slim/slim/Slim/Middleware/Flash.php(85): Slim\Slim->call() #5 /var/ www/html/api/v1/vendor/slim/slim/Slim/Middleware/MethodOverride.php(92): Slim\Middleware\Flash->call () #6 /var/www/html/api/v1/vendor/slim/slim/Slim/Slim.php(1271): Slim\Middleware\MethodOverride->cal l() #7 /var/www/html/api/v1/index.php(25): Slim\Slim->run() #8 {main} [] []

Here's line 20 of index.php

$results = ORM::for_table('cc_country')->raw_query('SELECT c.* FROM cc_country c JOIN 

ountrycodes d ON c.countryname LIKE CONCAT('%',d.country_Name,'%')')->find_many();

Any thoughts this would trigger division by zero error?

Eric

lplume commented 9 years ago

@ekoome (ref: https://github.com/j4mie/idiorm/issues/244 ) i think it's a problem about the string you pass to raw_query method (something like http://stackoverflow.com/questions/6978215/mysql-divison-by-zero-error )

ekoome commented 9 years ago

Fixed it by putting double quotes for the query: "Select .." instead of 'Select ...' Very obscure error. Thanks lplume for pointing me on the right direction.

bobdenotter commented 9 years ago

@ekoome: I think the error is not really obscure. You tried to use '%' inside a string that also had single quotes. You need to either escape them, or use 'the other kind'.

lplume commented 9 years ago

@bobdenotter your explanation is not explanatory. Using '%' inside a single quotes string (without nothing more) does not exaplain a div by zero itself, especially to someone who get this error: obviously not for those who have in-depth. But, if s/he is asking.. ew! Be real, never ever get a T_PAAMAYIM_NEKUDOTAYIM?

ffflabs commented 9 years ago

Since the % was unquoted, it was interpreted as a module operator, and the right parameter was eval'd as a real. Something like that.

However, even if this was an unexpected behavior (which is not) it's still not a Slim issue, but an ORM issue. BTW, which ORM is that? Eloquent? I can tell it isn't doctrine :horse:

lplume commented 9 years ago

@amenadiel you are right (by the way, a string in arithmetic operation is cast to (int) 0); @ekoome cross post the issue (see the reference above) because s/he not sure where it comes form (actually it is related to PHP syntax). The ORM is idiorm.