<tfoot id='xJ7U7'></tfoot>
  1. <legend id='xJ7U7'><style id='xJ7U7'><dir id='xJ7U7'><q id='xJ7U7'></q></dir></style></legend>

    <small id='xJ7U7'></small><noframes id='xJ7U7'>

    <i id='xJ7U7'><tr id='xJ7U7'><dt id='xJ7U7'><q id='xJ7U7'><span id='xJ7U7'><b id='xJ7U7'><form id='xJ7U7'><ins id='xJ7U7'></ins><ul id='xJ7U7'></ul><sub id='xJ7U7'></sub></form><legend id='xJ7U7'></legend><bdo id='xJ7U7'><pre id='xJ7U7'><center id='xJ7U7'></center></pre></bdo></b><th id='xJ7U7'></th></span></q></dt></tr></i><div id='xJ7U7'><tfoot id='xJ7U7'></tfoot><dl id='xJ7U7'><fieldset id='xJ7U7'></fieldset></dl></div>
      <bdo id='xJ7U7'></bdo><ul id='xJ7U7'></ul>

    1. 为什么我不能运行两个 mysqli 查询?第二个失败

      时间:2023-07-31

      <small id='ThK5a'></small><noframes id='ThK5a'>

        <legend id='ThK5a'><style id='ThK5a'><dir id='ThK5a'><q id='ThK5a'></q></dir></style></legend>

            <bdo id='ThK5a'></bdo><ul id='ThK5a'></ul>

              • <i id='ThK5a'><tr id='ThK5a'><dt id='ThK5a'><q id='ThK5a'><span id='ThK5a'><b id='ThK5a'><form id='ThK5a'><ins id='ThK5a'></ins><ul id='ThK5a'></ul><sub id='ThK5a'></sub></form><legend id='ThK5a'></legend><bdo id='ThK5a'><pre id='ThK5a'><center id='ThK5a'></center></pre></bdo></b><th id='ThK5a'></th></span></q></dt></tr></i><div id='ThK5a'><tfoot id='ThK5a'></tfoot><dl id='ThK5a'><fieldset id='ThK5a'></fieldset></dl></div>
                  <tbody id='ThK5a'></tbody>

                <tfoot id='ThK5a'></tfoot>
              • 本文介绍了为什么我不能运行两个 mysqli 查询?第二个失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                是否可以像这样有两个 mysqli 查询?

                Is it possible to have two mysqli queries like so?

                mysqli_query($dblink, "INSERT INTO images (project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name) VALUES ('$project_id', '$user_id', '$image_name', '$date_created', '$link_to_file', '$thumbnail', '$ImageName')");
                mysqli_query($dblink, "INSERT INTO images_history (project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name, day, month, year) VALUES ('$project_id', '$user_id', '$image_name', '$date_created', '$link_to_file', '$thumbnail', '$ImageName', '$day', '$month', '$year')");
                

                基本上我想更新我的数据库中的两个表.有没有更好的方法来做到这一点?

                Basically I want to update two tables in my DB. Is there a better way to do this?

                推荐答案

                mysqli_multi_query() 可以实现.

                示例:

                <?php
                
                $mysqli = new mysqli($host, $user, $password, $database);
                
                // create string of queries separated by ;
                $query  = "INSERT INTO images (project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name) VALUES ('$project_id', '$user_id', '$image_name', '$date_created', '$link_to_file', '$thumbnail', '$ImageName');";
                $query .= "INSERT INTO images_history (project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name, day, month, year) VALUES ('$project_id', '$user_id', '$image_name', '$date_created', '$link_to_file', '$thumbnail', '$ImageName', '$day', '$month', '$year');";
                
                // execute query - $result is false if the first query failed
                $result = mysqli_multi_query($mysqli, $query);
                
                if ($result) {
                    do {
                        // grab the result of the next query
                        if (($result = mysqli_store_result($mysqli)) === false && mysqli_error($mysqli) != '') {
                            echo "Query failed: " . mysqli_error($mysqli);
                        }
                    } while (mysqli_more_results($mysqli) && mysqli_next_result($mysqli)); // while there are more results
                } else {
                    echo "First query failed..." . mysqli_error($mysqli);
                }
                

                关键是,如果您想在一次调用中执行多个查询,必须使用 mysqli_multi_query.出于安全原因,mysqli_query 不会执行多个查询以防止 SQL 注入.

                The key is that you must use mysqli_multi_query if you want to execute more than one query in a single call. For security reasons, mysqli_query will not execute multiple queries to prevent SQL injections.

                还要记住 mysqli_store_result 的行为.如果查询没有结果集(INSERT 查询没有),它返回 FALSE 所以你还必须检查 mysqli_error 以查看它返回一个空字符串表示 INSERT 成功.

                Also keep in mind the behavior of mysqli_store_result. It returns FALSE if the query has no result set (which INSERT queries do not) so you must also check mysqli_error to see that it returns an empty string meaning the INSERT was successful.

                见:
                mysqli_multi_query
                mysqli_more_results
                mysqli_next_result
                mysqli_store_result

                这篇关于为什么我不能运行两个 mysqli 查询?第二个失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:为什么mysqli num_rows 总是返回0? 下一篇:MySQLi 相当于 mysql_result()?

                相关文章

                最新文章

              • <small id='hsauU'></small><noframes id='hsauU'>

              • <legend id='hsauU'><style id='hsauU'><dir id='hsauU'><q id='hsauU'></q></dir></style></legend>

                    <bdo id='hsauU'></bdo><ul id='hsauU'></ul>

                    <tfoot id='hsauU'></tfoot>
                    <i id='hsauU'><tr id='hsauU'><dt id='hsauU'><q id='hsauU'><span id='hsauU'><b id='hsauU'><form id='hsauU'><ins id='hsauU'></ins><ul id='hsauU'></ul><sub id='hsauU'></sub></form><legend id='hsauU'></legend><bdo id='hsauU'><pre id='hsauU'><center id='hsauU'></center></pre></bdo></b><th id='hsauU'></th></span></q></dt></tr></i><div id='hsauU'><tfoot id='hsauU'></tfoot><dl id='hsauU'><fieldset id='hsauU'></fieldset></dl></div>