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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:35:00+00:00 2026-05-16T12:35:00+00:00

Ok, I am completely baffled. I am setting up an OO site. I have

  • 0

Ok, I am completely baffled.

I am setting up an OO site. I have a class that defines all my database params, as follows:

$db->host= "localhost";
$db->name= "mydatabase";
$db->user= "user";
$db->pw = "password";

The class is being instantiated correctly and the values show up in pages that appear after this class has been loaded.

BUT, when I try to connect to this database from a different class, it does not connect. Here’s how I am connecting:

$dbconn = mysql_connect($db->host, $db->user, $db->pw);
mysql_select_db($db->name, $dbconn);

Everything works fine if I take out the user, pw and name variables and hard code in the correct values, but if any of them is referenced using the db construct, no connection happens. Again, the db construct appears just fine on other pages and I am seeing the variable values being presented correctly. The $db->host variable, however, always works.

Here’s is how I am constructing the db class:

class database {
    var $host;
    var $name;
    var $user;
    var $pw;

    function __construct($host = "localhost", $name = "mydatabase", $user = "user", $pw = "password"){
        $this->host =   $host;
        $this->name =   $name;
        $this->user =   $user;
        $this->pw =     $pw;
    }
}

and then I of course do

 $db = new database();

Thanks in advance for any help!

  • 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-16T12:35:01+00:00Added an answer on May 16, 2026 at 12:35 pm
    1. Don’t use PHP4
    2. Why don’t you just use PDO
    3. What’s the point of storing password or username as a object property?
    4. Probably the problem is a $db variable scope

    How to fix all of that?

    class MyClass {
        protected $db;
    
        public function __construct(PDO $db) {
            $this->db = $db;
        }
    
        public function doSth() {
            $this->db->query('..');
        }
    }
    
    $db = new PDO('mysql:dbname=mydatabase;host=localhost', 'user', 'password');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $obj = new MyClass($db);
    $obj->doSth();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am completely baffled by this as I have coded several other methods using
Here is a problem that has had me completely baffled for the past few
I'm using NetBeans for a project, and baffled that I seem completely unable to
Completely new to java and I have been playing around with regex in a
I am completely new to HTML5 and have been reading about it for the
I am completely new to Jena/TDB. All I want to do is to load
For some reason, the retain/release behavior in the following code has me completely baffled.
We have a new developer that is just now learning LinqToSql. While going over
I have a super-simple class representing a decimal # with fixed precision, and when
I have a databound ListBox that is behaving strangely. The ListBox's SelectionMode property is

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.