• <tfoot id='LltHf'></tfoot>

        <legend id='LltHf'><style id='LltHf'><dir id='LltHf'><q id='LltHf'></q></dir></style></legend>
          <bdo id='LltHf'></bdo><ul id='LltHf'></ul>
      1. <small id='LltHf'></small><noframes id='LltHf'>

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

        调用非对象 PHP 帮助上的成员函数 prepare()

        时间:2023-07-31

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

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

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

                <tfoot id='akNoV'></tfoot>

                1. 本文介绍了调用非对象 PHP 帮助上的成员函数 prepare()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试编写一个 PHP 函数.这很简单.这只是一个查询数据库的准备好的语句,但我无法让它工作.我不断收到错误 Call to a member function prepare() 在非对象上.这是代码:

                  I am trying to write a PHP function. It is very simple. It is just a prepared statement that queries the database, but I can not get this to work. I keep recieving the error Call to a member function prepare() on a non-object. here is the code:

                  $DBH = new mysqli("host", "test", "123456", "dbname");
                  function selectInfo($limit, $offset){
                      $stmt = $DBH->prepare("SELECT * FROM information LIMIT ?,?");
                      $stmt->bind_param("ii", $limit, $offset);
                      $stmt->execute();
                      }
                  selectInfo();
                  

                  每当我调用该函数时,我都会收到该错误.有人可以帮忙吗?

                  Any time I call the function i get that error. Can someone please help?

                  推荐答案

                  这是一个范围错误.您正在使 $DBH 成为一个全局变量.所以当你进入函数时,全局变量是不可用的.您有 5 种实​​际选择.

                  It's a scoping error. You're making $DBH a global variable. So when you enter the function, the global variable is not available. You have 5 real options.

                  1.使用全局关键字

                  function doSomething() {
                      global $DBH;
                      //...
                  

                  这不是一个好主意,因为它使维护和测试成为 PITA.想象一下尝试调试该函数调用.您现在需要找出 $DBH 的定义位置,以尝试弄清楚发生了什么......

                  This is not a good idea, since it makes maintenance and testing a PITA. Imagine trying to debug that function call. You now need to go find out where $DBH is defined to try to figure out what's going on...

                  2.将 $DBH 作为函数的参数

                  2. Make $DBH a parameter to the function

                  function doSomething(MySQLi $DBH) {
                  

                  它的优点是明确.但这仍然不是很好,因为调用代码需要跟踪全局变量.

                  It has the advantage of being explicit. But it's still not great since the calling code then needs to keep track of the global variable.

                  3.创建一个函数来获取"$DBH 对象

                  3. Create a function to "get" the $DBH object

                  function getDBH() {
                      static $DBH = null;
                      if (is_null($DBH)) {
                          $DBH = new mysqli(...);
                      }
                      return $DBH;
                  }
                  
                  function doSomething() {
                      $DBH = getDBH();
                  }
                  

                  这样做的好处是完全解决了全局变量问题.但是也很难有多个连接或将任何代码重用于其他连接.

                  This has the advantage of getting around the global variable problem completely. But it's also hard to have multiple connections or re-use any of the code for other connections.

                  4.创建一个类来包装数据库访问

                  class Database {
                      public function __construct($host, $user, $pass) {
                          $this->DBH = new MySQli($host, $user, $pass);
                      }
                      public function doSOmething() {
                          $this->DBH->foo();
                      }
                  }
                  

                  这为您封装了所有内容.所有数据库访问都将通过一个类进行,因此您无需担心全局变量访问或其他任何事情.

                  This encapsulates everything for you. All database access will go through a single class, so you don't need to worry about global variable access or anything else.

                  5.使用预先构建的类/框架

                  这是最好的选择,因为您无需担心自己动手.

                  This is the best option, since you don't need to worry about doing it yourself.

                  数据库访问类:

                  • 快速谷歌搜索以帮助您入门
                  • Doctrine ORM - 具有完整 ORM(对象映射)的完整数据库访问库
                  • ADODB - 一个与数据库无关的数据库访问库
                  • Pear MDB2 - 另一个数据库访问库
                  • A quick google search to get you started
                  • Doctrine ORM - A complete database access library with full ORM (Object Mapping)
                  • ADODB - A database agnostic database access library
                  • Pear MDB2 - Another database access library

                  完整框架:

                  • Zend 框架
                  • 锂框架
                  • 代码点火器
                  • (真的还有很多,我不打算再列出来了,因为那是另一个问题……)

                  真的,选择是无穷无尽的.找到你喜欢的东西,并坚持下去.它真的会让你的生活更轻松......

                  Really, the choices are endless. Find something you like, and stick with it. It really will make your life easier...

                  这篇关于调用非对象 PHP 帮助上的成员函数 prepare()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何解决“致命错误:找不到类‘MySQLi’"? 下一篇:获取带有 mysqli 结果的行数组

                  相关文章

                  最新文章

                2. <tfoot id='QBTYj'></tfoot>

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

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

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