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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:26:14+00:00 2026-05-20T10:26:14+00:00

I have created a file name database.php and here is it’s content <?php #Start

  • 0

I have created a file name database.php and here is it’s content

<?php
#Start Session
session_start();
#Start Output Buffering
ob_start();
#Set Default TimeZone
date_default_timezone_set('Asia/Kolkata');

#Define Connection Constant
define('HOST','localhost');
define('USERNAME','user');
define('PASSWORD','pass');
define('DATABASE','database');
//Define Configuration Constant
define('DATE', date("d-F-Y/H:ia"));

#Connect to the database
try
{
    #Define Connection String Using PDO.
    $DBH = new PDO('mysql:host='.HOST.';dbname='.DATABASE,USERNAME,PASSWORD);

    #Set Error Mode to ERRMODE_EXCEPTION.
    $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
    echo $e->getMessage();
    //Log Errors into a file
    file_put_contents("resources/logs/Connection-log.txt", DATE.PHP_EOL.$e->getMessage().PHP_EOL.PHP_EOL, FILE_APPEND);
}
?>

and now i have defined a PHP’s class and i have declared a method where i want to fetch some values based on the connection above. i have included the database.php file.

here is my code.

include('../../config/database.php');
class Property
{
    public function getAllCountries()
    {
        #Query Using Prepared Statement
        $STH = $DBH->query('SELECT * FROM countries');

        #Set the Fetch Mode
        $STH->setFetchMode(PDO::FETCH_ASSOC);

        $countries = array();
        while($row = $STH->fetch())
        {
            $countries[] = $row['name'];
            return $countries;
        }
    }
}
$property = new Property;
echo $property->getAllCountries();

when i initialize the class it doesn’t have any problem but when i try to call $property->getAllCountries(); method it gives me the following error.

Notice: Undefined variable: DBH in /Applications/MAMP/htdocs/kokaris/administrator/resources/library/models/class.property.php on line 8

Fatal error: Call to a member function query() on a non-object in /Applications/MAMP/htdocs/kokaris/administrator/resources/library/models/class.property.php on line 8

what is wrong with my code?

  • 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-20T10:26:14+00:00Added an answer on May 20, 2026 at 10:26 am

    The problem is that $DBH is not in scope at the time of the call $DBH->query(). $DBH is in the global scope, while the call is within a function’s scope (getAllCountries). Unlike some other languages, global variables are not accessible within functions unless you specifically declare them.

    The one-line workaround for this is to use the global value of $DBH within the function:

    public function getAllCountries()
    {
        global $DBH;
    
        #Query Using Prepared Statement
        $STH = $DBH->query('SELECT * FROM countries');
    

    However, this is probably a bad idea, because you have now locked yourself in to a particular way of handling database connections. It would be better to pass the database object as a parameter to the constructor, store it in an object variable, and retrieve it when you need to use it.

    class Property
    {
        protected $dbh;
    
        public function __construct($dbh) {
            $this->dbh = $dbh;
        }
    
        public function getAllCountries() {
            $STH = $this->dbh->query('SELECT * FROM countries');
    
            [snip]
        }    
    }
    

    You would then need to initialise your class with the DB object passed as a parameter:

    $property = new Property($DBH);
    echo $property->getAllCountries();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a batch file that was created for an Oracle database that I'm
I have an Oracle database backup file (.dmp) that was created with expdp .
I have created a Python file to generate a Mandelbrot set image. The original
I have created a C# class file by using a XSD-file as an input.
I've got two PostgreSQL databases that have been created using the same sql file.
I have a library I created, File mylib.c: #include <mylib.h> int testlib() { printf("Hello,
I have a berkeley db file (*.bdb) which is created by the C implementation(python
I have a single spool mbox file that was created with evolution, containing a
I am analyzing a .dmp file that was created and I have a call
I would like to create a file whose descriptor would have some customizable behavior.

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.