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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:37:09+00:00 2026-06-05T09:37:09+00:00

So I made a database class to handle all of my database requests. Everything

  • 0

So I made a database class to handle all of my database requests. Everything goes through the constructor and it should return values.

The class is so

<?php

class Database {

    /**
     * This array holds all of the configuration settings for the database
     * @var array
     */
    private $config = array(
        'username'  =>  '',
        'password'  =>  '',
        'host'      =>  '',
        'database'  =>  ''
    );

    /**
     * Holds the parameters passed to the class
     * @var mixed
     */
    private $parameters;

    /**
     * Database Handler
     * @var [type]
     */
    private $DBH;

    /**
     * Class constructor
     * @param [type] $action     [description]
     * @param [type] $parameters [description]
     */
    public function __construct($action, $parameters){
        $this->parameters = $parameters;

        $this->DBH = new PDO("mysql:host=".$this->config['host'].";dbname=".$this->config['database'], $this->config['username'], $this->config['password']); 

        return $this->$action();
    }


    private function query(){
        $STH = $this->DBH->prepare($this->parameters);
        $STH->execute();
        $result = $STH->fetchColumn();
        echo "<br><br>RESULT:".$result."<br><br><br>";
        echo "<br><br>RESULT:".empty($result)."<br><br><br>";

        return (empty($result)) ? FALSE : TRUE;
    }
} 

I removed everything bar the function giving issues. It is meant to return true or false. Instead the return value when I call $result = new Database('query', $query); is an object with a ton of data

Any idea what I have done wrong?

  • 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-05T09:37:11+00:00Added an answer on June 5, 2026 at 9:37 am

    PHP ignores what you return in __construct. If you create a new object with new then the new object is returned and not what the return in __construct says.

    To achieve what you want you have to create a new function which executes the action for you outside of the constructor – like that:

    class Database {
        // your code...
    
        public function __construct($parameters) {
            $this->parameters = $parameters;
    
            $this->DBH = new PDO("mysql:host=".$this->config['host'].
                ";dbname=".$this->config['database'],
                $this->config['username'],
                $this->config['password']); 
        }
    
        public function perform($action) {
            return $this->$action();
        }
    
        // rest of your code...
    }
    
    // usage:
    $db = new Database($query);
    $result = $db->perform('query'); // result should be a boolean.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've made POCO object mapped to database like this: class MoneyObject { Money MoneyAmmount
I was following this tutorial in order to integrate a pre made database to
I've made a database that has information like this http://i170.photobucket.com/albums/u244/therealbbri06/database.jpg and I'm trying to
This is not a connection timeout as a connection to the database is made
I have a database class that I made that uses PDO to connect to
Currently I'm programming a database class which makes a little bit use of PHP's
I want to create a class to handle everything to do with images for
In short I am writing a class handler to handle to database integration of
I made a database in PhpMyAdmin and developed an application in Visual Studio 2010
I made a database using phonegap[cordova 1.7.0] sqlite on IOS5. But I now want

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.