Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8108707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:11:50+00:00 2026-06-06T01:11:50+00:00

How can I open a database connection in user class , where I can

  • 0

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


?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:11:53+00:00Added an answer on June 6, 2026 at 1:11 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that can open up some other forms at the user's
how can I open an MS Access 2007 database with a password in c#
I can use the sql server management studio to open a sqlserver 2000 database,
If I can open a connection to an MS Access file in C#, how
I have a form that can open a sub form (with ShowDialog ). I
I have MySQL running such that I can open a client command line and
I have created a database with 8 tables in SQL Server Management Studio on
I have a page where a user can insert some order about some product.
I get the following error on my php pages that use database functions: Warning:
I am using a PHP session manager class that will handle user sessions in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.