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

2021-12-25 00:00:00 oop php mysql 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);

相关文章