We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Sql problem on raw

Hi ,

i have a interesting problem on raw sql query.I have a model and on it have a function named runAjaxQuery

public static function runAjaxQuery($sql)
     {
        // Base model
        $social = new Social();

        // Execute the query
        return new Resultset(null, $social, $social->getReadConnection()->query($sql)); 
        }

i send to this method this sql query:

"SELECT S.*,S.user_one as user_id,S.id,U.username,U.profile_photo 
FROM `users` U,`social` S
WHERE 
CASE

WHEN `user_one` = 6
THEN `user_two` = `user_id`
WHEN `user_two` = 6
THEN `user_one`= `user_id`
END

AND
(`user_one` = 6 OR `user_two` =6) Group by S.id ORDER BY S.id DESC LIMIT 20"

when i want to run it with passing arguments to method i got error that :

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 14 

but when i edit on like below:

public static function runAjaxQuery()
     {

        $sql = "SELECT S.*,S.user_one as user_id,S.id,U.username,U.profile_photo 
FROM `users` U,`social` S
WHERE 
CASE

WHEN `user_one` = 6
THEN `user_two` = `user_id`
WHEN `user_two` = 6
THEN `user_one`= `user_id`
END

AND
(`user_one` = 6 OR `user_two` =6) Group by S.id ORDER BY S.id DESC LIMIT 20";
        // Base model
        $social = new Social();

        // Execute the query
        return new Resultset(null, $social, $social->getReadConnection()->query($sql));

        }

there is no error.

Why its happening ?

Thanks

Can you set up the SQL profiler in your app and then show us what getSQLStatement() is outputting for the broken SQL statement?

https://phalcon-php-framework-documentation.readthedocs.org/en/latest/reference/db.html?highlight=profile#profiling-sql-statements