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 6070803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:58:30+00:00 2026-05-23T09:58:30+00:00

I have two classes : SQL Connection Class class MysqlDB { protected $_mysql; protected

  • 0

I have two classes :

SQL Connection Class

   class MysqlDB {

   protected $_mysql;
   protected $_where = array();
   protected $_query;
   protected $_paramTypeList;

public function __construct($host, $username, $password, $db) {
      $this->_mysql = new mysqli($host, $username, $password, $db) or die('There was a problem connecting to the database');
   }

   public function query($query) 
   {
    //...................... }

   public function __destruct() {
        $this->_mysql->close();
   }
}

User Class

class cUser {

    private $_name;
    private $_email;
    private $_password;

    public function __construct() {
        $this->_name = '';
        $this->_email = '';
        $this->_password = '';
    }

    public function getUser($username) {
        global $db;
        return $db->query("SELECT * FROM user where username='$username'");
    }


}


**index.php**
<?php
require_once('cMysqlDB.php');
require_once('cUser.php');

$db = new MysqlDB('host','username','password','db');
$user = new cUser();
$userData = $user->getUser('username');
print_r($userData);
?>

Totally works! Thanks to Sabeen Malik

  • 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-05-23T09:58:30+00:00Added an answer on May 23, 2026 at 9:58 am

    Though I am not entirely sure why you are inheriting the Users from the MySQL class. But anyway, if you really need to do it that way.

    Instead of

    $this->_conn = new MysqlDB($host, $username, $password, $db);
    

    do:

    parent::__construct($host, $username, $password, $db);
    

    You don’t need the $this->_conn just use $this->_mysql

    I believe the MysqlDB class would be in the singleton pattern. The User class would just use the methods of the MySQLDB class by talking to its object like :

    $results = MySQLDB::instance()->query(whatever);

    or something on those lines. Some people even use a global $db object and use that in their classes ( i am not condoning that though ) but there are several ways to go about this. I am not sure if you have chosen the right one.

    Edit:
    Using the global $db, you can get the user data like this:

    class cUser {
    
        private $_name;
        private $_email;
        private $_password;
    
        public function getUser($username) {
            global $db;
    
            return $db->query(whatever);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes declared like this: class Object1 { protected ulong guid; protected
I have two classes which both extend from SQL class like this: class SQL
I have two classes: class Foo{ public int FooId { get; set; } ...
I have two classes Car and Drivers : public class Car { public string
I have two classes: public class Parent { public virtual long? ID { get;
I have two classes: public class Foo { public int FooId {get;set;} public virtual
I have two entity classes: public class Invoice { public int ID { get;
I'm using LINQ to SQL and C#. I have two LINQ classes: User and
I have two classes, and want to include a static instance of one class
I have two classes, Foo and Bar, that have constructors like this: class Foo

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.