• <small id='22YT7'></small><noframes id='22YT7'>

  • <tfoot id='22YT7'></tfoot>

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

        实现mysqli连接的最佳实践/最实用的方法

        时间:2023-07-30
        <legend id='tVVlc'><style id='tVVlc'><dir id='tVVlc'><q id='tVVlc'></q></dir></style></legend>
      1. <tfoot id='tVVlc'></tfoot>
      2. <i id='tVVlc'><tr id='tVVlc'><dt id='tVVlc'><q id='tVVlc'><span id='tVVlc'><b id='tVVlc'><form id='tVVlc'><ins id='tVVlc'></ins><ul id='tVVlc'></ul><sub id='tVVlc'></sub></form><legend id='tVVlc'></legend><bdo id='tVVlc'><pre id='tVVlc'><center id='tVVlc'></center></pre></bdo></b><th id='tVVlc'></th></span></q></dt></tr></i><div id='tVVlc'><tfoot id='tVVlc'></tfoot><dl id='tVVlc'><fieldset id='tVVlc'></fieldset></dl></div>

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

                  <tbody id='tVVlc'></tbody>

                  <bdo id='tVVlc'></bdo><ul id='tVVlc'></ul>
                • 本文介绍了实现mysqli连接的最佳实践/最实用的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在努力简化我们的数据库助手和实用程序,我看到我们的每个函数,例如 findAllUsers(){....}findCustomerById($id) {...} 有自己的连接细节,例如:

                  I'm working on streamlining a bit our db helpers and utilities and I see that each of our functions such as for example findAllUsers(){....} or findCustomerById($id) {...} have their own connection details for example :

                  function findAllUsers() {
                      $srv = 'xx.xx.xx.xx';
                      $usr = 'username';
                      $pwd = 'password';
                      $db = 'database';
                      $port = 3306;
                      $con = new mysqli($srv, $usr, $pwd, $db, $port);
                  
                      if ($con->connect_error) {
                          die("Connection to DB failed: " . $con->connect_error);
                      } else {
                          sql = "SELECT * FROM customers..."
                          .....
                          .....
                      }
                  
                  }
                  

                  等每个助手/功能.所以我考虑使用一个返回连接对象的函数,例如:

                  and so on for each helper/function. SO I thought about using a function that returns the connection object such as :

                  function dbConnection ($env = null) {
                      $srv = 'xx.xx.xx.xx';
                      $usr = 'username';
                      $pwd = 'password';
                      $db = 'database';
                      $port = 3306;
                      $con = new mysqli($srv, $usr, $pwd, $db, $port);
                  
                      if ($con->connect_error) {
                          return false;
                      } else {
                          return $con;
                      }
                  }
                  

                  那我就可以了

                  function findAllUsers() {
                      $con = dbConnection();
                      if ($con === false) {
                          echo "db connection error";
                      } else {
                          $sql = "SELECT ....
                          ...
                      }
                  

                  与诸如 $con = new dbConnection() 之类的类系统相比,使用这样的函数有什么优势吗?

                  Is there any advantages at using a function like this compared to a Class system such as $con = new dbConnection() ?

                  推荐答案

                  您应该只打开一次连接.一旦你意识到你只需要打开一次连接,你的函数 dbConnection 就变得毫无用处了.您可以在脚本开始时实例化 mysqli 类,然后将其作为参数传递给所有函数/类.

                  You should open the connection only once. Once you realize that you only need to open the connection once, your function dbConnection becomes useless. You can instantiate the mysqli class at the start of your script and then pass it as an argument to all your functions/classes.

                  连接总是相同的三行:

                  mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
                  $con = new mysqli($srv, $usr, $pwd, $db, $port);
                  $con->set_charset('utf8mb4');
                  

                  然后只需将其作为参数传递,不再使用 if 语句执行任何检查.

                  Then simply pass it as an argument and do not perform any more checks with if statements.

                  function findAllUsers(mysqli $con) {
                      $sql = "SELECT ....";
                      $stmt = $con->prepare($sql);
                      /* ... */
                  }
                  

                  看起来您的代码是某种意大利面条式代码.因此,我强烈建议重写它并在 PSR-4 中使用 OOP.

                  It looks like your code was some sort of spaghetti code. I would therefore strongly recommend to rewrite it and use OOP with PSR-4.

                  这篇关于实现mysqli连接的最佳实践/最实用的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:致命错误:在非对象上调用成员函数 query() 下一篇:使用 MySQLi 进行 PHP 分页

                  相关文章

                  最新文章

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

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

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

                      <tfoot id='hNr58'></tfoot>
                        <bdo id='hNr58'></bdo><ul id='hNr58'></ul>