2

When executing a multi-command script (for instance, a few CREATE TABLE commands and perhaps a few INSERT commands), particularly with the MySQL PDO driver for PHP, the return is always 0 (without any error info) even if the script contains errors - syntax or logic.

This prevents you from running a long build script and then checking to see if the script was executed successfully or not. If an error occurs, the process terminates at the point where the script fails, but still exists with 0 and no error information.

I have searched this site and the net for an answer to this question, and it seems that the general response is "there's nothing you can do about it". My current solution is to run a "verify build" query that checks for some data and assesses the response to that - this seems terribly inefficient.

Has anyone come across this problem? If so, how have you ensured the build script was executed successfully?

1
  • Yes, I've come across this problem. It's a real pain. Because the there's no error info there's no way to know if and what went wrong. However, I've noticed, that PDO will actually return 0 on success and 1 on failure, but still no error info. If I could vote this question up twice I would. Commented Jun 5, 2012 at 11:24

4 Answers 4

1

Thanks for everyone's answers - I did not find a solution to this, instead ended up using Doctrine, which tied in nicely with database migration.

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

Comments

0

As a workaround, could you split the string that contains the script by ; and execute each statement individually?

You can then isolate any issues down to a particular statement.

At php.net, PDO::exec doesn't mention returning false on failure, but PDO::query does.

1 Comment

Thanks for your answer. I thought about this solution at first, but discarded because you would have to deal with pre-parsing the build.sql script for string literals that contain the ';' character. Surely PDO should be handling this?
0

Just create a stored function which will run your script, your function should return a value in case of a success and use a selection instead of execute. This way you'll know whether your script was successful. You can delete your installation function if the command terminated with success.

1 Comment

This sounds like an interesting work-around. Would I be able to create a stored function in the database that just "wraps" my build script? Would it be possible to put the whole build script in a transaction then, and then commit or role back depending on the result?
0

You can get the info for each separate command using a construct like:

do
{
    // do something with your current rowset
}
while($pdo->nextRowSet());

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.