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

    1. <legend id='Oli2N'><style id='Oli2N'><dir id='Oli2N'><q id='Oli2N'></q></dir></style></legend><tfoot id='Oli2N'></tfoot>
      • <bdo id='Oli2N'></bdo><ul id='Oli2N'></ul>

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

      1. pdo lastInsertId 返回零(0)

        时间:2023-10-05
        <tfoot id='SHU61'></tfoot>

            <legend id='SHU61'><style id='SHU61'><dir id='SHU61'><q id='SHU61'></q></dir></style></legend>
          • <small id='SHU61'></small><noframes id='SHU61'>

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

            • <bdo id='SHU61'></bdo><ul id='SHU61'></ul>

                    <tbody id='SHU61'></tbody>
                  本文介绍了pdo lastInsertId 返回零(0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  所有查询都成功执行,当我检查 MySQL 行中的表插入成功且没有任何错误,但 lastInsertId() 返回 0.为什么?

                  All queries execute successfully, when I check table in MySQL row inserted successfully without any error, but lastInsertId() returns 0. why?

                  我的代码:

                  // queries executes successfully, but lastInsetId() returns 0
                  // the menus table has `id` column with primary auto_increment index
                  // why lastInsertId return 0 and doesn't return actual id?
                  
                  
                  $insertMenuQuery = " 
                   SELECT @rght:=`rght`+2,@lft:=`rght`+1 FROM `menus` ORDER BY `rght` DESC limit 1; 
                   INSERT INTO `menus`(`parent_id`, `title`, `options`, `lang`, `lft`, `rght`) 
                        values 
                    (:parent_id, :title, :options, :lang, @lft, @rght);";
                       try {
                             // menu sql query
                             $dbSmt = $db->prepare($insertMenuQuery);
                  
                             // execute sql query
                             $dbSmt->execute($arrayOfParameterOfMenu);
                             // menu id
                             $menuId = $db->lastInsertId();
                  
                             // return
                             return $menuId;
                  
                       } catch (Exception $e) {
                            throw new ForbiddenException('Database error.' . $e->getMessage());
                       }
                  

                  推荐答案

                  我们必须使用 PDO_MySQL

                  With PDO_MySQL we must use

                  $DB->setAttribute(PDO::ATTR_EMULATE_PREPARES,TRUE); // there are other ways to set attributes. this is one
                  

                  以便我们可以运行多个查询,例如:

                  so that we can run multiple queries like:

                  $foo = $DB->prepare("SELECT * FROM var_lst;INSERT INTO var_lst (value) VALUES ('durjdn')");
                  

                  但遗憾的是,这样做可以减轻 $DB 返回正确插入 ID 的负担.您必须单独运行它们才能检索插入 ID.这将返回正确的插入 ID:

                  but sadly, doing so relieves the $DB from returning the correct insert id. You would have to run them separately to be able to retrieve the insert id. This returns the correct insert id:

                  $DB->setAttribute(PDO::ATTR_EMULATE_PREPARES,TRUE);
                  $foo = $DB->prepare("INSERT INTO var_lst (value) VALUES ('durjdn')");
                  $foo->execute();
                  echo $DB->lastInsertId();
                  

                  但这不会:

                  $DB->setAttribute(PDO::ATTR_EMULATE_PREPARES,TRUE);
                  $foo = $DB->prepare("SELECT * FROM var_lst;INSERT INTO var_lst (value) VALUES ('durjdn')");
                  $foo->execute();
                  echo $DB->lastInsertId();
                  

                  这甚至不会运行两个查询:

                  and this won't even run the two queries:

                  $DB->setAttribute(PDO::ATTR_EMULATE_PREPARES,FALSE); // When false, prepare() returns an error
                  $foo = $DB->prepare("SELECT * FROM var_lst;INSERT INTO var_lst (value) VALUES ('durjdn')");
                  $foo->execute();
                  echo $DB->lastInsertId();
                  

                  这篇关于pdo lastInsertId 返回零(0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PDO::FETCH_ASSOC 未获取所有内容 下一篇:MySQL 从带有 PDO 的 SELECT 插入

                  相关文章

                  最新文章

                    <bdo id='6LElw'></bdo><ul id='6LElw'></ul>

                  <legend id='6LElw'><style id='6LElw'><dir id='6LElw'><q id='6LElw'></q></dir></style></legend>

                  <i id='6LElw'><tr id='6LElw'><dt id='6LElw'><q id='6LElw'><span id='6LElw'><b id='6LElw'><form id='6LElw'><ins id='6LElw'></ins><ul id='6LElw'></ul><sub id='6LElw'></sub></form><legend id='6LElw'></legend><bdo id='6LElw'><pre id='6LElw'><center id='6LElw'></center></pre></bdo></b><th id='6LElw'></th></span></q></dt></tr></i><div id='6LElw'><tfoot id='6LElw'></tfoot><dl id='6LElw'><fieldset id='6LElw'></fieldset></dl></div>
                1. <small id='6LElw'></small><noframes id='6LElw'>

                  1. <tfoot id='6LElw'></tfoot>