1

Is there any class or library to php, for this task?

Thanks ;)

4
  • 4
    This is not a bad question, but some more background and detail would be helpful. Do you want to only test drive the query in general, or do you want to protect against SQL injection...? What SQL wrapper are you using, mysql, mysqli, PDO? Commented Sep 5, 2011 at 9:18
  • 5
    I guess not, since it isn't quite trivial to parse SQL, given all existing dialects of the language. Why would you want something like this? What's wrong with sending the query to MySQL and catch any errors that occur? :-) Commented Sep 5, 2011 at 9:19
  • I'm working of sql statement constructor. I'm a bit new to this, so there could be bugs and i need some kind of parser for sql validity after my sql assembler have finished building statement. Commented Sep 5, 2011 at 9:24
  • Well i guess i'll have to do it then via try catch :S. Ok thanks ;) Commented Sep 5, 2011 at 9:25

3 Answers 3

2

You could set up a local mySQL database with mock tables to have statement executed there first. This will take less resources than the real request and you can then check for errors. You could write a thin wrapper for that.

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

1 Comment

Yea guess i'll do it that way. Thanks.
2

If you want to check if query executes without errors you can call it within transaction and rollback in the end.

5 Comments

I don't want to process it at all if it's invalid. There should be some parser for this task.
@Beck Why would your SQL be invalid?
@Beck - there is some parser for this task. It's built into MySQL, and results in an error. Editing your question and including more information on why you want to do this, what you're trying to avoid, etc, would be quite useful.
I want t avoid mysql eating resources from invalid sql statements. Guess it's not so costly after all. Unwanted requests to mysql afterall. :)
It's probably more costly for you to replicate MySQL's ability to detect query errors than it is for MySQL to do it.
-1

You can prepare the sql statement first using prepare() function. If it return false, then there are errors. Otherwise, you are good to go.

Good luck.

1 Comment

I see this answer has 1 downvote, but what's actually wrong with relying on prepare?

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.