插入到唯一列时捕获错误并继续

发布于 2024-10-06 11:56:46 字数 240 浏览 3 评论 0原文

我有一个独特的专栏。据我了解,问题是,当您尝试向该列插入一些违反其唯一性的数据时,查询将停止处理。我想做的只是捕获错误并继续处理其余行。您将如何使用 PHP 和 MySQL 以最有效的方式做到这一点?

这里有一个简短的例子 http://pastie.org/1357001

我真正想知道的是,如果发生错误,循环会被破坏吗?

I have a unique column. The thing is, as I understand it, when you try to insert some data into that column that violates its uniqueness, the query will stop getting processed. What I wanna do is simply capture the error and continue with the rest of the rows. How would you do this in the most efficient way using PHP and MySQL?

here you have a short example
http://pastie.org/1357001

what i really was wondering is that will the loop get broken if an error occurs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月竹挽风 2024-10-13 11:56:46

在这一点上我和 ajreal 是一致的。违反约束不应导致 PHP 中出现异常。您可以只测试 mysqli_stmt::execute 的返回值,或您正在使用的任何东西,并处理发生的任何错误:

$mysqli = new mysqli('host', 'user', 'password', 'db');

// Prepare an INSERT;
$stmt = $mysqli->prepare('INSERT INTO mytable (id, data) VALUES (?, ?)');
$data = 'Some data';

// Insert 3 rows.
for ( $id = 1; $id <= 3 ; $id++ ) {

    // Attempt to insert each row twice.
    for ( $test = 1; $test <= 2; $test++ ) {

        // Bind the ID and data.
        $stmt->bind_param('is', $id, $data);

        // Execute the statement.
        if ( $stmt->execute() ) {
            // No error.
            echo "Inserted row ID $id\n";
        } else {
            // An error, but no exception.
            printf(
                "Error %d inserting row ID %d: %s\n",
                $stmt->errno,
                $id,
                $stmt->error
            );
        }
    }    
}

打印:

Inserted row ID 1
Error 1062 inserting row ID 1: Duplicate entry '1' for key 'PRIMARY'
Inserted row ID 2
Error 1062 inserting row ID 2: Duplicate entry '2' for key 'PRIMARY'
Inserted row ID 3
Error 1062 inserting row ID 3: Duplicate entry '3' for key 'PRIMARY'

I'm with ajreal on this one. Violating a constraint should not cause an exception in PHP. You can just test the return value from mysqli_stmt::execute, or whatever it is that you are using, and deal with any errors that have occurred:

$mysqli = new mysqli('host', 'user', 'password', 'db');

// Prepare an INSERT;
$stmt = $mysqli->prepare('INSERT INTO mytable (id, data) VALUES (?, ?)');
$data = 'Some data';

// Insert 3 rows.
for ( $id = 1; $id <= 3 ; $id++ ) {

    // Attempt to insert each row twice.
    for ( $test = 1; $test <= 2; $test++ ) {

        // Bind the ID and data.
        $stmt->bind_param('is', $id, $data);

        // Execute the statement.
        if ( $stmt->execute() ) {
            // No error.
            echo "Inserted row ID $id\n";
        } else {
            // An error, but no exception.
            printf(
                "Error %d inserting row ID %d: %s\n",
                $stmt->errno,
                $id,
                $stmt->error
            );
        }
    }    
}

Prints:

Inserted row ID 1
Error 1062 inserting row ID 1: Duplicate entry '1' for key 'PRIMARY'
Inserted row ID 2
Error 1062 inserting row ID 2: Duplicate entry '2' for key 'PRIMARY'
Inserted row ID 3
Error 1062 inserting row ID 3: Duplicate entry '3' for key 'PRIMARY'
奢望 2024-10-13 11:56:46

您应该能够捕获该错误:

try {
    // execute SQL query for one record
} catch(Exception $e) {
    // handle error here
}

You should be able to catch that error:

try {
    // execute SQL query for one record
} catch(Exception $e) {
    // handle error here
}
倾城°AllureLove 2024-10-13 11:56:46
foreach($somethings as $something)
{
    try
    {
       //mysql_query
    }
    catch(Exception $e)
    {
        $failed[] = $e;
        continue;
    }

    //process anything else.
}

例外是必经之路。

foreach($somethings as $something)
{
    try
    {
       //mysql_query
    }
    catch(Exception $e)
    {
        $failed[] = $e;
        continue;
    }

    //process anything else.
}

Exceptions are the way to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文