简体   繁体   English

PHP 不会为多个查询引发错误

[英]PHP not throwing error for multiple queries

I have some PHP code to execute multi queries and one of the queries has an error in it.我有一些 PHP 代码来执行多个查询,其中一个查询有错误。 But PHP is not detecting it.但是 PHP 没有检测到它。 Here's the code这是代码

        $updateCategoriesQ = "DELETE FROM category_products WHERE product = $id; INSERT INTO category_products (category, products) VALUES ";
        foreach ($product['categories'] as $key) {
            $updateCategoriesQ .= "(".mysqli_real_escape_string($connect, $key)."', $id), "; //error is here in the quote
        }
        $updateCategoriesQ = rtrim($updateCategoriesQ, ', ');
        o($updateCategoriesQ);

        $updateCategories = mysqli_multi_query($connect, $updateCategoriesQ);
        if($updateCategories){
            o('query ok'); //receives this output (wrapper function for echo)
        }

The query that forms is forms 的查询是

DELETE FROM category_products WHERE product = 1; INSERT INTO category_products (category, products) VALUES (1', 1), (2', 1)

The second query has an error but PHP says its ok.第二个查询有错误,但 PHP 说没问题。 If I create an error in the first query (delete) then it does throws the error but not if the error is in the second query.如果我在第一个查询(删除)中创建错误,那么它会抛出错误,但如果错误在第二个查询中则不会。 Is there a different method of capturing error here?这里有不同的捕获错误的方法吗?

In your code after mysqli_more_results($connect) (it return FALSE) you need call mysqli_next_results($connect) (also return FALSE) for retrieve errors.在 mysqli_more_results($connect) 之后的代码中(它返回 FALSE),您需要调用 mysqli_next_results($connect) (也返回 FALSE)来检索错误。

The documentation of mysqli_multi_query() says: mysqli_multi_query()的文档说:

Returns FALSE if the first statement failed.如果第一条语句失败,则返回FALSE To retrieve subsequent errors from other statements you have to call mysqli_next_result() first.要从其他语句中检索后续错误,您必须首先调用mysqli_next_result()

$updateCategories = mysqli_multi_query($connect, $updateCategoriesQ);
if($updateCategories){
    o('delete query ok');

    $insertCategories = mysqli_next_result($connect);
    if ($insertCategories) {
        o('insert query ok');
    } else {
        o('insert query failed: ' . mysqli_error($connect));
    }
} else {
    o('delete query failed: ' . mysqli_error($connect));
}

Okay, with the help of @Barmar and this answer here's a good way to capture errors on subsequent queries.好的,在@Barmar 的帮助下,这里的答案是捕获后续查询错误的好方法。

        $updateCategories = mysqli_multi_query($connect, $updateCategoriesQ);
        if($updateCategories){
            do {
                mysqli_next_result($connect);
                $result = mysqli_store_result($connect);
                if (!$result) {
                   o(mysqli_error($connect));
                }
            } while (mysqli_more_results($connect));
        } else {
            o(mysqli_error($connect));
        }

Do not use mysqli_multi_query() !不要使用mysqli_multi_query() You can only use this query if you have no user input and even then it is really cumbersome and unnecessary.只有在没有用户输入的情况下才能使用此查询,即使这样也非常麻烦和不必要。

Instead use prepared statements.而是使用准备好的语句。 This is the recommended and secure way.这是推荐且安全的方式。

// Add this before new mysqli() to enable error reporting
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$stmtDel = $connect->prepare('DELETE FROM category_products WHERE product = ?');
$stmtDel->bind_param('s', $id);
$stmtDel->execute();

$stmtInsert = $connect->prepare('INSERT INTO category_products (category, products) VALUES(?, ?)');
$stmtInsert->bind_param('ss', $key, $id);
foreach ($product['categories'] as $key) {
    $stmtInsert->execute();
}

As you can see here I have two separate queries and I execute the second one multiple times.正如您在此处看到的,我有两个单独的查询,并且多次执行第二个查询。 This is faster, more secure and it will throw all the errors.这更快,更安全,并且会抛出所有错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM