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

  • Home
  • SEARCH
  • 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 8876041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:07:12+00:00 2026-06-14T19:07:12+00:00

Hi guys I am having problem with my code. with __construct() I am getting

  • 0

Hi guys I am having problem with my code. with __construct() I am getting Fatal error: Call to a member function prepare() on a non-object but without it my code is working.

class Animals{

public $db_fields;

 public function __construct(){
  $this->db_fields = $this->get_fields();

  foreach($this->db_fields as $field){
  $this->$field = "";
 }


 public function get_fields(){
  global $dbh;

  $q = $dbh->prepare("DESCRIBE animals");
  $q->execute();
  $db_fields = $q->fetchAll(PDO::FETCH_COLUMN);

  return $db_fields;
 }
}
$f = new Animals();

/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'root';
/*** mysql password ***/
$password = '';
/*** mysql database***/
$dbname = 'animals';

try {
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database <br />';

$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}

I just want to make my fields(animal_id,animal_type,animal_name) work as same as

public $animal_id;
public $animal_type;
public $animal_name;
  • 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-14T19:07:13+00:00Added an answer on June 14, 2026 at 7:07 pm

    You have to instantiate the Animals object after you create $dbh, otherwise it’s not defined in the get_fields() function.

    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
    $f = new Animals();
    

    That being said, a better design is to use dependency injection, and send the $dbh object to the class, like this:

    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
    $f = new Animals( $dbh);
    

    So, your updated class would look like:

    class Animals{
    
        private $dbh;
    
        public function __construct( $dbh){
            $this->dbh = $dbh;
        }
    
        public function get_fields(){
            $q = $this->dbh->prepare("DESCRIBE animals");
        }
    }
    

    The benefit? You get rid of the global variable. Typically, requiring a global variable is a bad design, and you want to avoid it. You can see why just based on the problem you’re having – It requires a global state to be set in order to function.

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

Sidebar

Related Questions

hey guys having this really simple problem but cant seem to figure out have
Hi guys I'm having trouble getting my code to correctly display html within the
Guys I am having difficulties on retrieving struct member values after calling a function
I'm having a problem getting a pseudo class working with my code. The code
HI Guys, Here I am having problem that How can I post an image
Hey guys (and gals) I'm having a problem using Assembly GetExportedTypes() in .NET 4.0.
Hey guys, I'm having a problem with IE7, my menu always drops down behind
Hi guys i've never written a comparator b4 and im having a real problem.
Okay guys, basically the problem I'm having is this. I've been assigned to write
This is similar to an earlier problem I was having which you guys solved

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.