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;
}
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
Simple answer. What you are trying to do is not possible.
You need an SQL Parser to check if statements are valid.
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