Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 180 Vote(s) - 3.59 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to resolve SQL table with prefix in PhpStorm?

#1
I'm working on PhpStorm to develop my Prestashop websites and I can't resolve this issue. I work on localhost and successfully connected PhpStorm to my MySQL Server.

Now PhpStorm throws warnings like "unable to resolve table '${_DB_PREFIX_}cms'". Prestashop uses prefixes for table names and it seems PhpStorm can't resolve those tables with prefixes.

Is there a workaround for this ?

Here is a code exemple from Prestashop-1.6 sources :
<!-- language: lang-php -->

$sql = 'SELECT c.`id_cms`, cl.`meta_title`, cl.`link_rewrite`
FROM `'._DB_PREFIX_.'cms` c
INNER JOIN `'._DB_PREFIX_.'cms_shop` cs
ON (c.`id_cms` = cs.`id_cms`)
INNER JOIN `'._DB_PREFIX_.'cms_lang` cl
ON (c.`id_cms` = cl.`id_cms`)
WHERE c.`id_cms_category` = '.(int)$id_cms_category.'
AND cs.`id_shop` = '.(int)$id_shop.'
AND cl.`id_lang` = '.(int)$id_lang.
$where_shop.'
AND c.`active` = 1
ORDER BY `position`';

[![Error in PhpStorm for Mysql tables with prefixes][1]][1]


[1]:
Reply

#2
The reason why this isn't work is because you are most likely only loading one schema, you need to load the `information_schema.*`

To do this, go to the database tab in the top right and where you have added your MySQL database right click and select properties.

Now you'll have a screen called `Data Sources and Drivers`, it should open on a tab called `General`, click the third tab called `Schemas` and and add `information_schema.*` to this list of loaded Schemas.

Click apply and okay and then PhpStorm will now know your database structure and then be intelligently able to work with you, therefor removing all the errors.
Reply

#3
**Edit:** As [mentioned here](

[To see links please register here]

), this has been fixed in PhpStorm 2018.2, but *only for constants*.

----

I have a solution that doesn't involve throwing your IDE away. :)
However, a word of caution: it's an ugly hack™ that comes without guarantees.

1. Assuming you already have a connection to the db in PhpStorm, generate the ddl for the desired db (Right Click on the connection -> SQL Scripts -> Generate DDL to Clipboard):

![Imgur](
)

2. Paste the content into some sql file somewhere inside your project. You should probably gitignore this file.

3. Replace all the tables' prefix in this ddl file with the one from your code. Use the PhpStorm typehint as a guideline. For example `'._DB_PREFIX_.'cms` would become `${_DB_PREFIX_}cms`:

![Imgur](
)

Note that you may have to use backticks to avoid breaking sql syntax due to curly brackets.

4. Add the ddl to your phpstorm project:

![Imgur](
)

5. Everything should now work:

![Imgur](
)
Reply

#4
Add this comment above the **$sql** query.
/** @noinspection SqlResolve */

This will suppress the warning only for this statement.
Reply

#5
**Edit:** At time of writing (2016) there was no solution to this issue. But since 2018, as mentioned in [Christian's answer][1], you can now [use constants in SQL queries][2].

---

Actually there is no way to handle that. But you may disable inspection for such warning.

1. Open `File` > `Settings` > `Editor` > `Inspections`
2. Expand `SQL`
3. Uncheck `Unresolved reference`


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#6
For future readers, this is now supported:

[To see links please register here]


.phpstorm.meta.php
```
<?php

namespace PHPSTORM_META {
override(
// Virtual function to indicate that all SQL
// injections will have the following replacement rules.
sql_injection_subst(),
map([
'{' => "", // all `{` in injected SQL strings will be replaced with a prefix
'}' => '', // all `}` will be replaced with an empty string
]));
}
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through