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

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

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

    2. <legend id='yxbC3'><style id='yxbC3'><dir id='yxbC3'><q id='yxbC3'></q></dir></style></legend>

        如何访问另一个页面上另一个类中的mysqli连接?

        时间:2023-07-30

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

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

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

                    <tbody id='P8Fyq'></tbody>
                  本文介绍了如何访问另一个页面上另一个类中的mysqli连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何在用户类中打开数据库连接,在那里进行数据库操作?以及为什么需要在 DBConnection 类中定义内置的创建函数..????

                  我已经创建了

                  1. db.php
                  2. user.php
                  3. result.php

                  在 db.php

                  class DBConnection{受保护的 $mysqli;私人 $db_host="127.0.0.1";私人 $db_name="测试";私人 $db_username="root";私人 $db_password="";公共函数 __construct(){$this->mysqli=new mysqli($this->db_host,$this->db_username,$this->db_password,$this->db_name) 或 die($this->mysqli->error);返回 $this->mysqli;}public function query($query)//为什么我需要创建这个函数{返回 $this->mysqli->query($query);}公共函数 real_escape_string($str){返回 $this->mysqli->real_escape_string();}函数 __destruct(){//关闭连接$this->mysqli->close();}}?>

                  在 User.php

                  mysqli=$conn;}公共函数 addUser($name,$username,$email,$pwd){$query="";$res=$this->mysqli->query($query);//请查一下DBConnection中的查询函数,需要定义什么查询>DBConnection 中的函数?返回 $res;}}?>

                  在 result.php

                   

                  <块引用>

                   $name = $conn->real_escape_string(trim(strip_tags($_POST['name'])));$username = $conn->real_escape_string(trim(strip_tags($_POST['username'])));$email = $conn->real_escape_string(trim(strip_tags($_POST['email'])));$password = $conn->real_escape_string(trim(strip_tags($_POST['pass'])));

                  //echo $name."".$username."".$email."".$password;$uObj=新用户($conn);$uObj->addUser($name,$username,$email,$password);echo "你好,你的名字是<I>".$name."</I>并且你的电子邮件ID是<I>".$email."</I>";}?>

                  解决方案

                  你的 DBConnection 类需要一个额外的方法:

                  公共函数getLink(){返回 $this->mysqli;}

                  看来您原来的 User 类是 DBConnection 的子类,因为 DBConnection 上的 mysqli 属性是protectedUser 类有一个 parent::__construct() 调用.

                  最好使用依赖注入,这样您的 User 类将通过构造函数接收其数据库连接:

                  公共函数__construct(DBConnection $db){$this->mysqli = $db->getLink();}

                  然后你可以从你的代码运行:

                  $db = 新的 DBConnection;$uObj = 新用户($db);

                  How can I open a database connection in user class , where I can do database operation? and why need to define inbuilt created functions in DBConnection class ..????

                  I have created

                  1. db.php
                  2. user.php
                  3. result.php

                  in db.php

                  class DBConnection  
                  {       
                    protected $mysqli;
                    private  $db_host="127.0.0.1";
                    private  $db_name="test";
                    private  $db_username="root";
                    private  $db_password="";
                  
                    public function __construct()
                      {
                          $this->mysqli=new mysqli($this->db_host,$this->db_username,
                                  $this->  db_password,$this->db_name) or die($this->mysqli->error);
                  
                           return $this->mysqli;
                      }
                  
                  public function query($query)  // why i need to creat this function
                       {
                      return $this->mysqli->query($query);
                       }
                  
                  public function real_escape_string($str)
                   {
                      return $this->mysqli->real_escape_string();
                   }
                  
                  
                     function __destruct(){
                       //Close the Connection
                       $this->mysqli->close();
                      }
                  }
                  ?>
                  

                  in User.php

                  <?php
                    require "db.php"; 
                  
                    class User {  
                  
                    public function __construct($conn)
                  {
                      $this->mysqli=$conn;
                  
                  }
                  
                     public function addUser($name,$username,$email,$pwd)
                     {
                       $query="  ";
                  
                       $res=$this->mysqli->query($query);
                  
                    //pls chek the query function in DBConnection,what is the need to define query               >       function in DBConnection ?
                  
                      return $res;
                      }
                     }    
                  ?>
                  

                  in result.php

                      <?php
                  
                        require "user.php";
                  
                        $conn=new DBConnection();
                  
                        if(isset($_POST['submit']))
                    {
                  

                   $name = $conn->real_escape_string(trim(strip_tags($_POST['name'])));
                     $username =  $conn->real_escape_string(trim(strip_tags($_POST['username'])));
                     $email =     $conn->real_escape_string(trim(strip_tags($_POST['email'])));
                     $password    = $conn->real_escape_string(trim(strip_tags($_POST['pass'])));
                  

                  //echo $name."".$username."".$email."".$password;
                    $uObj=new User($conn);
                    $uObj->addUser($name,$username,$email,$password);
                  
                  
                  echo " hi your name is <I>".$name."</I> and your email ID is <I>".$email."</I>";
                          }
                  
                  
                  ?>
                  

                  解决方案

                  Your DBConnection class would need an additional method:

                  public function getLink()
                  {
                      return $this->mysqli;
                  }
                  

                  It seems that your original User class was a subclass of DBConnection, because mysqli property on DBConnection is protected and User class has a parent::__construct() call.

                  It's better to use dependency injection, so your User class will receive its database connection via the constructor:

                  public function __construct(DBConnection $db)
                  {
                      $this->mysqli = $db->getLink();
                  }
                  

                  Then from your code you can run:

                  $db = new DBConnection;
                  $uObj = new User($db);
                  

                  这篇关于如何访问另一个页面上另一个类中的mysqli连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

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