r/PHPhelp • u/GuybrushThreepywood • Sep 05 '26
Using variable $placeholders breaks PHPStorm syntax/resolve
Hello all,
Looking for help on a long standing problem I have. I appreciate this is not a support forum for PHPStorm, however I thought that people here might have more experience.
PHPStorm provides really valuable syntax checking, table/column resolving inside mysql queries. If I accidentally type the orders table as oreders, it will highlight in red.
However when I include a PHP variable inside the query, the syntax/resolve checking completely stops. Example code:
$query = <<<MYSQL
SELECT
contracts.start_at
FROM
contracts
WHERE
contracts.id IN ( $placeholders )
MYSQL;
$result = $this->conn->execute_query(
$query,
[
...$contractIds,
],
);Without the $placeholders, PHPStorm will alert me to any misspelled table or column names.
Are there any options to resolve this? I have considered using sprintf( $query, $placeholders) but wondered if there was a better solution.
4
u/obstreperous_troll Sep 05 '26
You could do
contracts.id IN (?)then substitute the single placeholder with your generated list of them. Ultimately this is the kind of thing you'll want to use a query builder for: with DBAL you can bind an array to the query as a parameter and it will generate the proper SQL to make it work.2
u/colshrapnel Oct 21 '26
By the way, your question made me aware of this behavior, and I ended up using makeshift custom placeholders. Looked up some obscure function and ended up using OCTET_LENGTH(1) for a placeholder that I would strtr later.
2
u/allen_jb Sep 05 '26
I'm not 100% sure this will pick up this case, but I think it should. Test it out! (I've also never used it with mysqli, only PDO)
You should be able to integrate this with PHPStorms PHPStan integration to provide in-ide reporting.
1
u/GuybrushThreepywood Sep 11 '26
Apparently it is a bug:
Issue: https://youtrack.jetbrains.com/issue/WI-75697
1
u/Big-Dragonfly-3700 Sep 05 '26
For this specific case, if you use FIND_IN_SET() instead of an IN () comparison, you can simply use one prepared query place-holder in the query.
0
u/p1ctus_ Sep 05 '26
Difficult to detect for the ide. Switch to prepared statements, it's easier to read, write and detect.
1
u/TemplerTreiff 5d ago
using sprintf with %s is actually the cleanest way to handle this. the IDE knows how to ignore format specifiers inside SQL strings so you keep all your table and column autocompletion
0
u/cursingcucumber Sep 05 '26 edited Sep 05 '26
You can add a comment, instructing phpstorm of what language it is. It is now confused between php and mysql.
https://www.jetbrains.com/help/phpstorm/using-language-injections.html
-1
u/[deleted] Sep 06 '26
[removed] — view removed comment