SQL-PHP IntelliSense helps PHP projects write MySQL queries with schema-aware completions, lightweight linting, field hovers, and a quick action for running selected SQL.
The extension connects to your MySQL database, reads table and column metadata, and uses that schema while you edit SQL strings in PHP files.
- Table-name completion in supported MySQL query strings.
- Field-name completion when the source table can be inferred.
- Diagnostics for table and field names that do not exist in the connected database.
- Hover information for known fields, including the MySQL column type.
- Command palette actions for connecting to MySQL, linting the active file, and clearing stored credentials.
- Code action for running a selected SQL query and viewing results in a VS Code webview.
SQL extraction currently targets string literals passed to these static calls:
Database::prepare("SELECT id, name FROM users");
Database::getResults("SELECT * FROM users");
Database::getValue("SELECT email FROM users WHERE id = :id");
Database::getRow("SELECT * FROM users WHERE id = :id");
Database::PrepareExecuteTC("SELECT * FROM users");Current limitations:
- Queries must be quoted string literals.
- The extension is optimized for MySQL.
- Advanced SQL syntax, aliases, and dynamically constructed queries may not always be understood.
Below is a conceptual example of the completions, diagnostics, and hovers in action:
// 1. Table Name Completion
Database::prepare("SELECT * FROM |");
// ^ Autocomplete triggers: suggests tables ('users', 'products', 'orders')
// 2. Field Name Completion
Database::prepare("SELECT users.| FROM users");
// ^ Autocomplete triggers: suggests fields from 'users' table
// 3. Diagnostics & Error Highlighting
Database::prepare("SELECT invalid_field FROM users");
// ~~~~~~~~~~~~~ Diagnostic Error: Field name 'invalid_field' not found in table 'users'To provide fast and context-aware SQL tooling directly within PHP files, the extension implements the following pipeline:
- SQL Extraction (Pattern Matching):
The extension scans the PHP files using optimized regular expressions matching specific static query execution patterns (such as
Database::prepare(...)). - Context Resolution (Lexical Parsing):
When autocompletion is triggered, a custom lexical parser evaluates the SQL string preceding the cursor. It tracks whitespace, punctuation, and keyword structures to identify whether the cursor is in a
tablecontext or afieldcontext, and automatically maps aliases to their respective tables. - AST Construction (SQL Parsing):
The extension passes extracting queries to
node-sql-parserto construct an Abstract Syntax Tree (AST). By analyzing this AST, it resolves the referenced tables and fields to validate query correctness. - Schema Inspection & Caching: Using the configured connection credentials, the extension queries the MySQL database's schema metadata. It caches table names and field maps in-memory to ensure autocomplete suggestions and hover info are displayed with sub-millisecond response times.
- Visual Studio Code
1.84.0or newer. - Access to a MySQL database whose schema should power completions and linting.
- Install the extension.
- Open VS Code settings and configure:
SQL-PHP.Intellisense.database.hostSQL-PHP.Intellisense.database.portSQL-PHP.Intellisense.database.name
- Run
SQL-PHP: Connect to MySQL Databasefrom the command palette. - Enter the database username and password when prompted.
Credentials are stored with VS Code SecretStorage. Run SQL-PHP: Delete Database Credentials to clear them.
| Command | Description |
|---|---|
SQL-PHP: Connect to MySQL Database |
Connects to the configured MySQL database and loads schema metadata. |
SQL-PHP: Lint MySQL Queries |
Lints SQL queries in the active PHP document. |
SQL-PHP: Delete Database Credentials |
Removes the stored username and password. |
| Setting | Default | Description |
|---|---|---|
SQL-PHP.Intellisense.database.host |
localhost |
MySQL server host name or IP address. |
SQL-PHP.Intellisense.database.port |
3306 |
MySQL server port. |
SQL-PHP.Intellisense.database.name |
empty | Name of the MySQL database to inspect. |
npm install
npm run compile
npm run lint
npm testPackage the extension locally with:
npm run vsceFor manual installation from a VSIX file, see the VSIX installation guide.
- Configurable PHP function/method patterns for SQL extraction.
- Better support for single-quoted and multiline SQL strings.
- Workspace-wide linting for PHP files.
- Broader SQL parser coverage for joins, aliases, and dynamic query fragments.
Issues and pull requests are welcome on the GitHub repository.
MIT
With over 5,000+ active installations on the VS Code Marketplace, this extension is driven entirely by community utility. If this tool saves you a few context-switches or protects you from a broken query deployment today, please consider supporting its ongoing development:
- Star this repository to improve its visibility on GitHub so other developers can discover it.
- Leave a review on the VS Code Marketplace sharing your favorite feature or setup.
Found a bug or have a feature request? Please feel free to open an issue or submit a pull request. Your feedback helps make local SQL context mapping better for everyone.
