Is there any Regular Expression to check if a string is valid SQL? It must be PHP compatible. My code.
if(!preg_match("regular expression", $_POST['sql_input']){
echo "Please enter valid SQL.";
exit;
}
You could try this library: http://code.google.com/p/php-sql-parser/. I've not used it yet so I can't guarantee it but the code looks like it will be able to tell the difference between valid and invalid SQL.
Another option could be to use transactions if your SQL variant allows it. A transaction would allow you to execute the SQL and then cancel it afterwards reversing any damage that was done. I think I would prefer option 1 though.
I am quoting Godwin. Source
DROP DATABASE.No, it is impossible. But if you want to protect yourself against SQL injection attacks, there are other mechanisms you should use.
See for example: https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php
Simple answer. What you are trying to do is not possible.
You need an SQL Parser to check if statements are valid.
0's and 1's anything is possible nowadays ;-). I made a script using an array of keywords, and if one was found, then it would do whatever I told it to do. It could easily be modified to suit the OP's wish. Btw, I did NOT downvote this. It's not my style. ;-)in_array() @charlesisjan - No regex used.
DELETE FROM users;andDROP DATABASE databaseare valid queries ;)