开发者

PHP MySQL PDO::exec raises or throws no error for multi-command scripts

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 开发者_如何学JAVAchecking 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?


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.


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.


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.


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜