q2a / question2answer

Question2Answer is a free and open source platform for Q&A sites, running on PHP/MySQL.
http://www.question2answer.org/
GNU General Public License v3.0
1.63k stars 628 forks source link

Unable to query JSON inside SQL statement when using external users. #933

Closed mikewcaldwell closed 2 years ago

mikewcaldwell commented 2 years ago

I'm currently setting up Q2A to work with an existing user database where my user sessions database (built on node.js:express-session) uses JSON to store information about the session in a database.

When trying to read this data, I would like to be able to query JSON inside my SQL query (simple example follows, code is in qa-external-users.php::qa_get_logged_in_user()). The below query works when called directly through mysql.

'SELECT data->'$.cookie' FROM sessions WHERE session_id=#;'

However, in qa-db.php::qa_db_apply_sub, ^, $ and # are reserved with no way to escape them (as far as I can tell).

Would it be better to use mysqli bind_param and the built-in support for prepared statements? https://www.php.net/manual/en/mysqli-stmt.bind-param.php

Thanks.

pupi1985 commented 2 years ago

That's part of 1.9, but it has not been released. You can try to extract the new query logic from there, if you have some free time :)

Other approaches:

mikewcaldwell commented 2 years ago

That's good to hear, thanks for the info and quick response! I will go ahead and close this issue.