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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:31:42+00:00 2026-05-28T06:31:42+00:00

Problem: I have 2 classes, DB class and a User class, that will work

  • 0

Problem: I have 2 classes, DB class and a User class, that will work if placed in the same class but when i seperate them i cant figure out how to get the user class to connect using the DB class.

I have ‘DBinterface’ class and a ‘user’ class.

***dbinterface.php

<? class dbinterface {
    var $dbHost,
        $dbUser,
        $dbName,
        $dbPass,
        $dbUserTable;


    function User() {
        $this->dbHost = 'host':
        $this->dbUser = 'user';
        $this->dbName = 'name';
        $this->dbPass = 'pass';
        $this->dbUserTable = 'table';
    }

} // End dbinterface class definition ?> 

***user.php

<?
include('dbinterface.php');
    class User {

        var $userID,
            $userName,
            $userPassword;

        function registerUser($userName, $userPassword) {
            // Connect to database
            $dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
            if(!$dbLink) die("Could not connect to database. " . mysql_error());

            // Select database
            mysql_select_db($this->dbName);

            // Insert data
            $query = "insert into $this->dbUserTable values (NULL, \"$userName\", \"$userPassword\")";
            $result = mysql_query($query);

            // Test to make sure query worked
            if(!$result) die("Query didn't work. " . mysql_error());

            // Get the user ID
            $this->userID = mysql_insert_id();

            // Close database connection
            mysql_close($dbLink);

            // Assign the values to the data members
            $this->userName = $userName;
            $this->userPassword = $userPassword;
        } // End registerUser() ?>

PS:I have removed security and other mumbojumbo for easy legibility.
any and all help is MUCH appriciated!

  • 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-28T06:31:43+00:00Added an answer on May 28, 2026 at 6:31 am

    Well. I’ll try to imagine what are you wanted to do 🙂
    You need one class where you will store db variables, such as username, password and so on and 2nd class which will work with db-class params.

    /* file1 */

    Class db {
    
        private $_dbLink;
        private $_dbHost = 'host';
        private $_dbUser = 'user';
        private $_dbName = 'name';
        private $_dbPass = 'pass';
        private $_dbUserTable = 'table';
    
        public function connect ()
        {
            $this->_dbLink = mysql_connect($this->_dbHost, $this->_dbUser, $this->_dbPass);
            if(!$this->_dbLink) 
                throw new Exception ("Could not connect to database. " . mysql_error());
        }
    
        public function getLink()
        {
            return $this->_dbLink;
        }
    
        public function getUserName ()
        {
            return $this->dbUser;
        }
    
        public function getUserPass ()
        {
            return $this->_dbPass;
        }
    
        /* create the same methods for each variable */
    }
    

    /* file2 */

    Class User {
    
        public function registerUser (/* params */)
        {
            $db = new db();
            $db->connect();
    
            /* your code */
            // Select database
            mysql_select_db($db->getDb(), $db->getLink());
    
            // Insert data
            $query = "insert into ".$db->getTable()." values (NULL, \"$userName\", \"$userPassword\")"; /* here is SQL-injection */
            $result = mysql_query($query, $db->getLink());
            /* and so on ...... */
        }
    }
    

    /* file3 */

    // include file1
    // include file2
    $user = new User;
    $user->registerUser(/* params */);
    

    This is fast solution. You should learn how to create classes and project architecture. Use php.net for more information. Read about Object-oriented programming. And read about SQL-injections for safer code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Database and Class User . My problem is that I
I have three classes and a user control that will use these three classes.
Ok I have two modules, each containing a class, the problem is their classes
Problem: I have two spreadsheets that each serve different purposes but contain one particular
I'm designing a namespace to hold a set of classes that will handle user
I have a Java program that loads thirdparty class files (classes I did not
So I have this problem in PHP, I have a class called unit that
I have a few questions related to the design of my User class but
I have problem with base classes in WPF. I try to make a base
i have problem with autorotate on iphone i set up in all classes -

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.