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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:18:23+00:00 2026-05-31T21:18:23+00:00

In PHP, I have following Singleton Database Class: class Database { private static $instance;

  • 0

In PHP, I have following Singleton Database Class:

class Database
{
    private static $instance;

    private function __construct()
    {
        self::$instance = new mysqli('localhost', 'root', 'Matthias', 'financecontrol', '3307');
        if (!self::$instance) {
            throw new Exception('Could not connect to database in function __construct.');
        }
    }

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = new Database();
        }
        return self::$instance;
    }

}

Whenever I try to perform a query on the database in another PHP file, for example to check whether a user already exists:

function userExists($username)
    {
        try {
            $connection = Database::getInstance();

            $result = $connection->query("select * from user where username='$username'");
            if (!$result) {
                throw new Exception("Connection to database failed in function userExists.");
            }
            if ($result->num_rows > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception $ex) {
            $errorPager = new ErrorpageGenerator();
            $errorPager->generateErrorPage($ex->getMessage());
            return false;
        }
    }

I get an error message “PHP Fatal error: Call to undefined method Database::query() in User.php on line 44”

I’ve tried adding a query function in the Database class, but that did not seem to fix the problem. Any ideas? Thanks

  • 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-31T21:18:25+00:00Added an answer on May 31, 2026 at 9:18 pm

    You have to add this method of course. But you cannot assign Database() and the mySQLi object to m_pInstance

    so do:

    class Database
    {
      private static $conn;
    
      // ...
      public function __construct() 
      {
         self::$conn = new mysqli('localhost', 'root', 'root', 'database', '3307');
         //...
    

    and then

    public function query($sql)
    {
      return self::$conn->query($sql);
      // or 
      return mysqli_query(self::$conn, $sql);
    }
    

    EDIT

    Working code:

    class Database
    {
      private static $instance = null;
    
      private static $conn;
    
      private function __construct()
      {
        self::$conn = new mysqli('localhost', 'root', 'root', 'database', '3307');
      }
    
      public static function getInstance()
      {
        if (self::$instance == null) {
          self::$instance = new Database();
        }
    
        return self::$instance;
      }
    
      public function query($sql)
      {
        return self::$conn->query($sql);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Singleton Class in PHP. class CounterBalance{ private static $instance; private
I have following php class class User implements IUser { public function __construct($i_objParent =
I have following PHP code $val=<div id=user.$row['cid']. userid=.$row['cid']. class=innertxt><img src=images/images.jpg width=50 height=50><strong>.$uname.</strong><ul> <li>Email: .$row['cemail'].</li>
In my bootstrap.php I have the following: if($_SERVER['SERVER_NAME'] == 'localhost') Kohana::$environment = 'development'; else
I have following data to save in database using php my data is :
Hi I am quite new to php but i have been following some tutorials
I have following code (php), it will match img-src and replace with new url
I have a basic singleton class, similar to the one shown in the PHP
I have following PHP code: if($_SESSION['msg']['login-err']) { echo '<p class=error>'.$_SESSION['msg']['login-err'].'</p>'; unset($_SESSION['msg']['login-err']); } Everything working
I have following php function, that creates an array of all the days between

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.