Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

php - How to access mysqli connection in another class on another page?

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>";
        }


?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...