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

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

      <tfoot id='T1Pw3'></tfoot>
        <bdo id='T1Pw3'></bdo><ul id='T1Pw3'></ul>
      1. <i id='T1Pw3'><tr id='T1Pw3'><dt id='T1Pw3'><q id='T1Pw3'><span id='T1Pw3'><b id='T1Pw3'><form id='T1Pw3'><ins id='T1Pw3'></ins><ul id='T1Pw3'></ul><sub id='T1Pw3'></sub></form><legend id='T1Pw3'></legend><bdo id='T1Pw3'><pre id='T1Pw3'><center id='T1Pw3'></center></pre></bdo></b><th id='T1Pw3'></th></span></q></dt></tr></i><div id='T1Pw3'><tfoot id='T1Pw3'></tfoot><dl id='T1Pw3'><fieldset id='T1Pw3'></fieldset></dl></div>
      2. 函数中的 Mysqli 查询 - PHP

        时间:2023-07-31

          <bdo id='9a6cj'></bdo><ul id='9a6cj'></ul>

        • <small id='9a6cj'></small><noframes id='9a6cj'>

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

                <tfoot id='9a6cj'></tfoot>
                • 本文介绍了函数中的 Mysqli 查询 - PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在 functions.php 中有一个函数列表.我正在从 Mysql 升级到 Mysqli 因为我刚刚学习的 Mysql 现在已经贬值了.

                  I have a list of functions in functions.php. I'm upgrading from Mysql to Mysqli because I just learning Mysql is now depreciated.

                  我在顶级connect.php 文件中声明了我的连接.需要第一个文件.

                  I declare my connection in a top levelconnect.php file. The first file required.

                  无论如何回到我所有的函数都使用 mysql_query("QUERY") 并且总是工作正常的点.现在我将它们全部更改为:

                  Anyway back to the point all my functions use mysql_query("QUERY") and that always worked fine. Now I changed all them to:

                  $con->query("QUERY") // ($con is my connection variable)
                  

                  现在我得到一个

                  致命错误:在第 241 行调用 C:wampwwwPHPfunctions.php 中非对象的成员函数 query()

                  Fatal error: Call to a member function query() on a non-object in C:wampwwwPHPfunctions.php on line 241

                  如果我在整个文件中声明我的变量,我不明白为什么我可以查询.它应该可以在任何地方访问,我只是不确定.这使我的网站暂停,直到我能解决这个问题.这是来自 functions.php

                  I don't get why I can query if I'm declaring my variable in my whole file. It should be accessible everywhere, I'm just not sure. This has put my website on hold until I can fix this. Here is a sample function from functions.php

                  function getSiteName()
                  {
                      $row = $con->query("SELECT * FROM siteinfo")->fetch_array();
                      return $row["siteName"];
                  }
                  

                  我的连接:

                  global $con ;
                  $con = new mysqli("localhost", "itunes89", "XXXX","projectanvil") or die("Sorry, were having server connection issues. Please try again later.");
                  

                  推荐答案

                  那是一个 变量作用域问题.您需要将 $conn 传递给 getSiteName():

                  That's a variable scope problem. You need to pass $conn to getSiteName():

                  function getSiteName($con) {
                      $row = $con->query("SELECT * FROM siteinfo")->fetch_array();
                      return $row["siteName"];
                  }
                  
                  $name = getSiteName($con);
                  

                  或者使用带有构造函数注入的类:

                  Or using a class with constructor injection:

                  class MyThing
                  {
                      protected $con;
                  
                      public function __construct($con) {
                          $this->con = $con;
                      }
                  
                      public function getSiteName() {
                          $row = $this->con->query("SELECT * FROM siteinfo")->fetch_array();
                          return $row["siteName"];
                      }
                  }
                  
                  $obj = new MyThing($con);
                  $name = $obj->getSiteName();
                  

                  在整个课程中,您可以使用 $this->con 来访问连接.

                  Throughout the class you can use $this->con to access the connection.

                  这篇关于函数中的 Mysqli 查询 - PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:致命错误:调用未定义的方法 mysqli_result::fetch_al 下一篇:mysqli + xdebug 断点后关闭语句导致很多警告

                  相关文章

                  最新文章

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

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

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

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

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