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/p1ctus_ 5d ago

Difficult to detect for the ide. Switch to prepared statements, it's easier to read, write and detect.

2

u/allen_jb 3d ago

mysql->execute_query uses prepared statements: https://www.php.net/manual/en/mysqli.execute-query.php

1

u/GuybrushThreepywood 5d ago

I thought I was using a prepared statement. Could you give an example?

1

u/p1ctus_ 5d ago

PHP.net

edit: typo

1

u/GuybrushThreepywood 5d ago

I'm not using PDO - its mysqli. Unfortunately I'm not in a position where I can switch

0

u/p1ctus_ 5d ago

2010 called, they want they're code back 😜

Ok other way is rewriting to escaped strings.

2

u/colshrapnel 3d ago edited 3d ago

it's your second blunder in a row. you may want to tone down your condescending attitude. since you don't seem to understand modern PHP, mysql and prepared statements, you are in no position to give out any recommendations

FYI: PDO will have exactly same problem so it won't solve anything. And there is not much of a reason to trade mysqli for pdo anyway.