简体   繁体   中英

Same join query: two different results through Phalcon and phpMyAdmin

I am working with php Phalcon framework.

Query executed with phalcon:

$rawQuery = 
    'SELECT users.ID, users.Name, users.ProfilePictureUrl, users.Birthday, GetAge(users.Birthday) as Age, ' .
    '    users.Sex, users.LookingFor, commons.Name as CommonName, provinces.Name as ProvinceName, users.LastOnline FROM favorites ' .
    'INNER JOIN users ON favorites.FavoriteUserID = users.ID ' .
    'INNER JOIN commons ON commons.ID = users.CommonID ' .
    'INNER JOIN provinces ON provinces.ID = commons.ProvinceID ' .
    'WHERE favorites.OriginUserID = ' . $this->ID;
return $this->getReadConnection()->query($rawQuery)->fetchAll();

This query returns all the fields correctly, except CommonName, which should be a string 'Aglié', but is null. Sample output:

[{"ID":"2","0":"2","Name":"Olga","1":"Olga","ProfilePictureUrl":"asd","2":"asd","Birthday":"2014-07-09","3":"2014-07-09","Sex":"f","4":"f","LookingFor":"girls","5":"girls","CommonName":null,"6":null,"ProvinceName":"Torino","7":"Torino","LastOnline":null,"8":null}]

On the other hand, the same query executed through phpMyAdmin:

SELECT users.ID, users.Name, users.ProfilePictureUrl, users.Birthday, GetAge(users.Birthday) as Age,
    users.Sex, users.LookingFor, commons.Name as CommonName, provinces.Name as ProvinceName, users.LastOnline FROM favorites
INNER JOIN users ON favorites.FavoriteUserID = users.ID 
INNER JOIN commons ON commons.ID = users.CommonID
INNER JOIN provinces ON provinces.ID = commons.ProvinceID
WHERE favorites.OriginUserID = 1

it hangs indefinitely. However, if I take out the Age field, it returns all the correct fields, including CommonName (a valid string). Sample output:

ID, Name, ProfilePictureUrl, Birthday, Sex, LookingFor, CommonName, ProvinceName, LastOnline
2, Olga, asd, 2014-07-09, f, girls, Agliè, Torino, NULL

GetAge(birthday) is defined as follows:

return DATE_FORMAT(FROM_DAYS(DATEDIFF(curdate(),birthday)), '%Y')

You can assume for simplicity that there are only 4 tables users, favorites, commons and provinces, with only those fields used in the query. Anything other is not useful.

It makes absolutely no sense to me... Please help!

Apache version 2.4.4

PHP version 5.4.12

MySql version 5.6.12

Phalcon version 1.3.2

Windows 7 64-bit operative system

It was because of the special character 'é' at the end of the common name. Apparently json_encode skips that field if it contains such characters. I resolved by escaping special characters directly in the commons table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM