3

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


?>
mkat
  • 149
  • 1
  • 3
  • 12
  • It's not clear where `User` class gets its `->mysqli` property from. Is `User` a subclass of `DBConnection`? – Ja͢ck Jun 20 '12 at 05:25

1 Answers1

1

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);
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • but to do insertion operation i am not able to fire query inside user class in insert function. >addUser($name,$username,$email,$pwd) > { > > $query=" "; > > $res=$this->mysqli->query($query); > //i want to fire inser query here .... but i am not able to to do that > return $res; > } – mkat Jun 20 '12 at 05:22
  • @user1055480 your question is vague, but I've updated my answer, hope it helps. – Ja͢ck Jun 20 '12 at 05:28
  • the another way is that.
     u pass object of db while creating object of User `$uOBj=new User(new DBCOnnection); `is it correct way to get connection..???
    – mkat Jun 20 '12 at 05:34
  • @user1055480 I don't know why you added `query` and `escape` methods on your `DBConnection`; they don't make sense; just use `getLink()` as I suggested so that you have direct access to `mysqli`. – Ja͢ck Jun 20 '12 at 06:16
  • still i am not able to acess inbuilt method when i am trying >$name=$uObj->real_escape_string($name); >i am getting error "Fatal error: Call to undefined method User::real_escape_string()" – mkat Jun 20 '12 at 06:32