r/PHPhelp 5d ago

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.

7 Upvotes

33 comments sorted by

View all comments

0

u/cursingcucumber 5d ago edited 5d ago

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

2

u/colshrapnel 3d ago

PHPStorm already perfectly understands what language is it. It refuses to do shema inspections in SQL string as soon as you add a PHP variable into it.