1

I already reviewed most of the topics related to my concern

mysql explain delete?

detect mysql errors without executing it

Most of the examples are just using Select statements. Mine is about UPDATE and DELETE.

Is there a way to get the possible error return using PHP to check the update/delete query before execution?

My concern is about foreign key constraint.

I already have Php script before but now I guess will try to use EXPLAIN.

Before I was using mysql_error() to get the errors but of course the query must be executed to get errors and translate it to understandable message.

Lets say, how I can I use "Explain Delete ..." statement to get some information about errors?
or is "Explain" the right tool?

Query: Delete From project .

I have another table called project_assignment with index FK to Project_ID of project table and has data on it.

Right now, I only got this

id  | select_type | table | type |  possible_keys | key |   key_len |   ref |   rows |  Extra
1   |SIMPLE       |NULL   | NULL |  NULL          | NULL|   NULL    |   NULL|2       |  Deleting all rows
6
  • 2
    How can something know if it will get an error until it tries? Commented May 5, 2015 at 3:56
  • 3
    EXPLAIN is for showing how the query optimizer plans to perform the query. Why would you expect it to have anything to do with detecting errors? Commented May 5, 2015 at 3:57
  • @Barmar .. I was thinking that as well but as I've mentioned above, some of the comments on related topics states about using Explain.. and that confuses me on how to apply or use it. So, you mean its not possible to use Explain? thnx Commented May 5, 2015 at 4:00
  • 1
    stop trying to break the laws of physics Commented May 5, 2015 at 4:01
  • 1
    The other question is just talking about detecting syntax errors, not errors related to the data. Commented May 5, 2015 at 4:05

1 Answer 1

3

EXPLAIN is not the right tool for the job.

Take a look at using MySQL Transactions. This allows you to execute a series of queries 'optimistically' then commit the transaction. When you do so, you will be informed of any errors. If an error occurs, the entire transaction would be rolled back.

I imagine most uses for 'explain' before an UPDATE or DELETE query could be solved by transactions.

In pure SQL, this looks like:

START TRANSACTION;
SELECT ...;
UPDATE ...;
DELETE ...;
COMMIT;

Using PHP mysqli, you can use mysqli::begin_transaction, mysqli::commit, and mysqli::rollback.

This excellent StackOverflow answer, PHP + MySQL transactions examples shows how to use transactions in mysqli. The answer explains how to write a transaction in PHP mysqli like so:

try {
    $db->beginTransaction();

    $db->query('SELECT ...');
    $db->query('UPDATE ...');
    $db->query('DELETE ...');

    $db->commit();
} catch (Exception $e) {
    $db->rollback();
}
Sign up to request clarification or add additional context in comments.

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.