1

I'm using a PHP script to batch insert data into a MySQL table. How do I make the script skip any queries that throw an error and continue with the next. For example if field slug accepts unique values and I attempt to insert the same value twice it will throw a duplicate entry for key slug error and the script will stop executing.

How can I make it continue to the next statement?

Here a code example:

for ($row = $begin_row; $row <= $highestRow;  $row++) {
    $sql = sprintf("INSERT INTO ".$dbprefix."terms (name, slug, term_group) VALUES (%s, %s, %s)",
       GetSQLValueString($name, "text"),
       GetSQLValueString($slug, "text"), 
       GetSQLValueString(0, "int"));
    $result = mysql_query($sql) or die(mysql_error());
}
2
  • 1
    Do not use mysql_, use mysqli_ now, the old versions is deprecated Commented Jun 7, 2013 at 13:16
  • check before inserting duplicate entry is their. Commented Jun 7, 2013 at 13:27

2 Answers 2

4

Just delete code or die(mysql_error());

When you get mysql_error, you will do nothing and go to next query.

for ($row = $begin_row; $row <= $highestRow;  $row++) {
    $sql = sprintf("INSERT INTO ".$dbprefix."terms (name, slug, term_group) VALUES (%s, %s, %s)",
       GetSQLValueString($name, "text"),
       GetSQLValueString($slug, "text"), 
       GetSQLValueString(0, "int"));
    $result = mysql_query($sql); //possible error thrown, but we will do nothing in this case
}
Sign up to request clarification or add additional context in comments.

6 Comments

An SQL error can throw an exception that will terminate the script regardless. You probably want a try..catch clause there to catch and defuse the exception.
Can you provide an examlpe of a scenario where an SQL error will cause the script to fail? And why using a try...catch prevents this?
@MaximKumpan You are right in principle, but like most PHP extensions, the old (and deprecated) mysql extension does not throw exceptions, it simply returns false (and raises warnings and errors as appropriate). The newer mysqli does throw exceptions, though, and PDO can be set to either behaviour.
I never said it WILL, I said it CAN. The CodeIgniter db class does throw exceptions. I never used vanilla mysql_ but thought it should be worth mentioning. Apologies for the poorly worded comment, however.
@IMSoP True. A unified error handling mechanic is not a simple thing to design. I gotta admit, it's one of the weaker sides of my own projects.
|
2

How do I make the script skip any queries that throw an error

Doing that would be a really bad idea. If the queries are throwing an error then either there's bad data or your code is wrong.

Looking at the code you've provided (it doesn't batch the inserts) it will throw an error for a duplicate key - which may not be a valid fault in the processing - to deal with this, you should amend your code:

$sql = sprintf("INSERT IGNORE INTO "...

While there may be legitimate reasons for ignoring other errors, you should only explicitly ignore errors that you know are valid cases, e.g.

    if (!$result = mysql_query($sql)) {
        $err=mysql_errno();
        $msg="error " . $err . "\nin $sql\n\n" . mysql_error();
        if (!array_key_exists($err, $aceptable_error) {
           die("Failed: $msg");
        } else {
           print "warning: $msg";
        }
    }

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.