0

I have a php file which performs a series of insert queries. If any of the queries generates an error, I would like to return the error message and query string and roll back all the queries

So far I have this:

mysql_query("SET autocommit=0;");
mysql_query("BEGIN;");
$sql ="SOME MALFORMED QUERY";

mysql_query($sql);

if(mysql_error()){
    mysql_query("rollback;");
    $arr = array("returnCode" => 0, "returnMessage" => "Query failed: " .$sql. mysql_error());
    echo json_encode($arr);
    die();
}

However, In javascript all I'm seeing returned in the return message JSON field is 'Query failed: '. Any idea why this is?

4 Answers 4

1

The problem is with your ROLLBACK query: as explained in the manual, mysql_error returns the error text from the last MySQL function. Since you used mysql_query again, the previous error is lost.

I suggest this code:

mysql_query("SET autocommit=0;");
mysql_query("BEGIN;");
$sql ="SOME MALFORMED QUERY";

mysql_query($sql);
$error = mysql_error();
if($error){
    mysql_query("rollback;");
    $arr = array("returnCode" => 0, "returnMessage" => "Query failed: $sql, Error: $error");
    echo json_encode($arr);
    die();
}

Executing ROLLBACK in case an error occurs seems useless. The manual explains: Rolling back can be a slow operation that may occur implicitly without the user having explicitly asked for it (for example, when an error occurs)

The following code should then be enough:

mysql_query("SET autocommit=0;");
mysql_query("BEGIN;");
$sql ="SOME MALFORMED QUERY";

mysql_query($sql);
if(mysql_error()){
    $arr = array("returnCode" => 0, "returnMessage" => "Query failed: $sql, Error: ".mysql_error());
    echo json_encode($arr);
    die();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this makes sense. Although, I wonder why the $sql variable is blank as well?
Maybe this wasn't entirely clear: I had posted only the modified part of your code. I just updated my answer by including the full modified version.
0

Shouldn't

$arr = array("returnCode" => 0, "returnMessage" => "Query failed: " .$sql. sql_error());

be

$arr = array("returnCode" => 0, "returnMessage" => "Query failed: " .$sql. **mysql_error()**);

1 Comment

sorry! it should be that, but I made an error when copying the code over. I've edited the question
0

Probably the expression "Query failed: " .$sql. sql_error() is not well formed. The correct form is:

"Query failed: " . $sql . mysql_error()

Comments

0

I'd use PDO and its built-in transactions support.

<?php

// create PDO instance as $db here
// don't forget to set error mode to 'exception'

try {
    $db->beginTransaction();
    // do your queries here, i.e. $db->exec('BOGUS');
    $db->commit();
}
catch (Exception $e) {
    $db->rollBack();
    echo 'Error: ' . $e->getMessage();
}

It will give you any error encountered as the exception message when it throws one.

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.