1

I'm working on my own database management system, developped in PHP, and I've chosen the same syntax as the MySQL queries for my own queries.

I'd like to know if there was a tool to check that a MySQL query is valid, without having to connect to a real MySQL database.

Does someone have a way to do it ? I've though about using some regular expressions but I'm not sure this is the easiest (and fastest) way to do this.

7
  • 3
    I would try and find a library - a quick look shows me that mysql SQL parsing code is about 16,000 lines of C and C++ - thats a lot of work to redo Commented Nov 16, 2011 at 23:00
  • In fact I don't need to recode the parsing for all the queries, but only for the most common ones such as SELECT, INSERT, DROP or DELETE. Commented Nov 16, 2011 at 23:01
  • Your building a db management app? Like phpMyAdmin? Commented Nov 16, 2011 at 23:02
  • It's not exactly a real dbm app, just a little tool spend some time working on something. It's just a PHP program that will slightly simulate the functions of a real database. Commented Nov 16, 2011 at 23:05
  • How are you building your queries? If you're creating them by passing fields and data into functions then the validation of your queries largely relies on the integrity of function building it. Get that function right and you should rarely come into a problem. Regardless, attempting to execute the query on an actual database will provide you with a specific syntax message, which is vital for debugging. Commented Nov 16, 2011 at 23:07

3 Answers 3

5

There's a few good PHP SQL parsers that break down the query into structured arrays.

You could run the code through the parser and see if it breaks to determine whether it's valid syntax.

http://code.google.com/p/php-sql-parser/

and

http://pear.php.net/package/SQL_Parser

are 2 I have used in the past.

Sign up to request clarification or add additional context in comments.

Comments

2
set @s := 'your sql script';
prepare stm1 from @s;

2 Comments

You should give more explanation and more formatting. It's your first time here, so you should pay more attention on how to answer
This is the best answer so far. Unfortunately single-quotes must be escaped, which means editing the target script and potentially introducing errors - but this is better than using a non-mysql engine (regex, php) to parse the script.
1

a tool like that can't ensure tables or columns exist. the tool needs database schema for checking errors. I use heidisql and If I had more money ı would buy right to have more than one database on mysql server and I would execute the "create database" or copy database code and test on the second database which doesn't have any values, or prefereably, with values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.