简体   繁体   中英

How can I check a MySQL query syntax?

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.

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.

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

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.

I believe there is something you could try in phpmyadmin to test your code out, without affecting your database.

REGEX would be too much work... Maybe create a dummy table and try the syntax there?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM