<tfoot id='b72XV'></tfoot>

  • <legend id='b72XV'><style id='b72XV'><dir id='b72XV'><q id='b72XV'></q></dir></style></legend>
      <bdo id='b72XV'></bdo><ul id='b72XV'></ul>

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

        PDO 全局实例 新的 PDO ,它可能吗?

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

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

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

                    <tbody id='vAch9'></tbody>
                  本文介绍了PDO 全局实例 新的 PDO ,它可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我使用 PHP PDO 连接到我的数据库并运行一些查询,然后在某些表单上使用查询返回.

                  I am using PHP PDO to connect to my database and run some querys to then use the query return on some forms.

                  也就是说,我有一个选择,它由来自查询的值填充.

                  That is, I have a select where it is populated by the values ​​coming from the query.

                  我创建了两个函数来连接数据库和数据,但我想知道我是否可以创建一个全局变量,因为我使用的是New PDO".

                  I created two functions to connect to the database and data, but I wanted to know if I can create a global variable because I am using the "New PDO" .

                  /** Conecta com as impressas **/
                  function impressoras()
                  {       
                      $PDO2 = new PDO('mysql:host=localhost;dbname=ti','root','xxx');
                      $PDO2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                      global $stmt;
                      $stmt = $PDO2->prepare("SELECT * FROM league");
                      $stmt->execute();
                      $result = $stmt->fetchAll();
                      return $result; 
                      echo json_encode($user_arr);    
                  }
                  function carrefour()
                  {
                      $PDO3 = new 
                      PDO('mysql:host=localhost;dbname=ti','root','xxx');
                      $PDO3->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                      global $stmt;
                      $stmt = $PDO3->prepare("SELECT * FROM lol");
                      $stmt->execute();
                      $result = $stmt->fetchAll();
                      return $result;     
                      echo json_encode($user_arr);    
                  }
                  

                  推荐答案

                  您可以以简单的旧程序风格非常简单地共享"同一个数据库连接对象 (PDO) :-) 这是一个简单的例子:

                  You could "share" the same database connection object (PDO) very simple in plain old procedural style :-) Here is a simple example:

                  // config.php
                  define('DB_DSN', 'mysql:host=127.0.0.1;dbname=test;charset=utf8');
                  define('DB_USERNAME', 'root');
                  define('DB_PASSWORD', '');
                  
                  // database.php
                  function db()
                  {
                      static $db = null;
                      if ($db === null) {
                          $db = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD, array(
                              PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                              PDO::ATTR_PERSISTENT => false,
                              PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                              PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8 COLLATE utf8_unicode_ci"
                          ));
                      }
                      return $db;
                  }
                  

                  用法

                  function test1()
                  {       
                      $pdo = db();
                      $stmt = $pdo->prepare("SELECT * FROM league");
                      $stmt->execute();
                      $result = $stmt->fetchAll();
                      return $result;     
                  }
                  

                  如果您更喜欢更专业的解决方案,请查看 PHP-DI 并使用依赖注入.

                  If you prefer a more professional solution then take a look an PHP-DI and use dependency injection.

                  这篇关于PDO 全局实例 新的 PDO ,它可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在PHP中使用PDO时如何检索最后插入行的id? 下一篇:使用 php pdo 删除我的数据库中的行

                  相关文章

                  最新文章

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

                  1. <tfoot id='lQ2I0'></tfoot>

                  2. <legend id='lQ2I0'><style id='lQ2I0'><dir id='lQ2I0'><q id='lQ2I0'></q></dir></style></legend>
                    • <bdo id='lQ2I0'></bdo><ul id='lQ2I0'></ul>

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