Auto-complete in mysql queries doesnt pick up right away

Hi guys,
I`m new to phpStorm and it is simply awesome IDE but i have the following problem.

I have manage to connect to local database via data sources and wanted to test the Auto-completion for MySQL queries.
It actualy works but it doesnt pick up right away. I have to type 2-3 words of my query for phpStorm to recognize that I am typing a query.

For example:

 
$result = $mysql-> query('Select id, username, pass from users');
  
Untill i get after "from" it doesnt understand that i`m writing a MySQL query and it doesnt autocompletes for me...
 
After that it works fine but it is a big problem for me.
  
Is there a way to tell phpStorm that i`m writing a query? What am i doing wrong?
 
3 comments
Comment actions Permalink

Hi there,

Yes, IDE must detect that you are actually typing/have SQL code there (for SELECT queries it watches for "SELECT *** FROM" part) as it does not inject other languages into function parameters automatically.

My only suggestion here is to use intermediate variable where your current SQL Dialect can be injected straight away, for example by using specific HEREDOC:

$sql = <<<SQL
...TYPE HERE
SQL;
$result = $mysql->query($sql);


About Language Injections: https://confluence.jetbrains.com/display/PhpStorm/Language+Injections+%28mix-ins%29+in+PhpStorm

P.S.
In next major version (PhpStorm v9) you will be able to forcibly inject specific language at any string (i.e. something like $result = $mysql-> query(/** @inject MySQL */ 'Select id, username, pass from users'); ) .. but using HEREDOC is still preferred (looks better, bit easier to manage, easier to debug as allows to see command in separate variable, especially if query assembled dynamically etc)

Surelly, you can manually inject language into any string even right now (see #1.2 of the above manual) .. but that info is stored temporarily and will not be available on next session (which is good enough for most cases though).

0
Comment actions Permalink

Thank you!,
That was realy helpful. Glad to know that in next version we will have a way to force SQL injection. This will make things much easier to see when working with prepared statements. 

I think this is better:

$query = $database->prepare(/** @inject SQL */'SELECT * FROM users WHERE id = ?', array('1'))->execute()->fetch_all_assoc();


than:

 
$sql = <<< SQL
SELECT * FROM users WHERE id = ?;
SQL;

$query
= $database->prepare($sql, array('1'))->execute()->fetch_all_assoc();
  
Thanks again :)
0
Comment actions Permalink
Glad to know that in next version we will have a way to force SQL injection. This will make things much easier to see when working with prepared statements.

As I have mentioned -- you can inject any language into any string manually even right now (the #1.2 from the manual). The forcing is useful if such injection has to be done permanently e.g. when IDE does not inject there language at all (useful for regex, for example) or if it injects wrong language (e.g. HTML while content is preferred to be treated as plain text)

I think this is better:

It is indeed OK for short queries (like you have showed). Try making a query that has join or two or select 10 fields out of 20 in a table -- query spreaded across few lines will be easier to understand/edit.

0

Please sign in to leave a comment.