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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:41:10+00:00 2026-05-20T09:41:10+00:00

Is it something incomplete/ incorrect in the error class function below? I can never

  • 0

Is it something incomplete/ incorrect in the error class function below? I can never get any error message from it when a query is incorrect,

#connects the database and handling the result
class __database {

    protected $connection = null;
    protected $error = null;

    #make a connection
    public function __construct($hostname,$username,$password,$database)
    {
        $this -> connection = new mysqli($hostname,$username,$password,$database);

        if (mysqli_connect_errno()) 
        {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }
    }

    #fetches all result rows as an associative array, a numeric array, or both
    public function fetch_all($query) 
    {
        $result = $this -> connection -> query($query);
        if($result) 
        {

            return $result -> fetch_all(MYSQLI_ASSOC);
        } 
        else
        {
            $this -> error = $this -> connection -> error;
            return $this -> error;
        }
    }

    #display error
    public function get_error() 
    {
        return $this -> error;
    }

    #closes the database connection when object is destroyed.
    public function __destruct()
    {
        $this -> connection -> close();
    }
}

public function get_error() seems to be useless in my db class… I have read about php Exception but I don’t know how to incorporate it into the this db class above! please advise…

EDIT:

I tried to change the code into this,

# return the current row of a result set as an object
    public function fetch_object($query) 
    {
        $result = $this->connection->query($query);
        if($result)
        {
            ...
        }
        else
        {
            __database::get_error();
        }
    }

and the error class function,

#display error
    public function get_error() 
    {
        $this->error = $this->connection->error;
        return $this->error;
    }

So I thought it should trigger the get_error() function, but still nothing has been displayed from the error function…

  • 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-20T09:41:10+00:00Added an answer on May 20, 2026 at 9:41 am

    Firstly, if you want to take full advantage of the mysqli class then you should extend it, and just overriding where needed.

    class Database extends mysqli
    {
        public function __construct($host = null,$username= null,$password = null,$database = "",$port = null, $socket = null)
        {
             $host     = $host     !== null ? $host     : ini_get("mysqli.default_host");
             $username = $username !== null ? $username : ini_get("mysqli.default_user");
             $password = $password !== null ? $password : ini_get("mysqli.default_pw");
             $port     = $port     !== null ? $port     : ini_get("mysqli.default_port");
             $socket   = $socket   !== null ? $socket   : ini_get("mysqli.default_socket");
    
             /*
                 Perform any custom actions here!
             */
    
             parent::__construct($host,$username,$password,$database,$port,$socket);
        }
    
        public function fetch_all($query) /*Overridden*/
        {
            if(false !== ($result = parent::query($query))) //run directly in mysqli
            {
                 return $result->fetch_all(MYSQLI_ASSOC);
            }
    
            return false;
        }
    
        public function get_error() 
        {
            if($this->errno || $this->error)
            {
                return sprintf("Error (%d): %s",$this->errno,$this->error);
            }
        }
    }
    

    This will allow mysqli to handle the errors and allow you to easily access them, there is not point in creating a class that would just mimic a class, your better extending the class itself.

    Secondly its bad practice to space out your code, it should not effect the way php interpretes the code but it can confuse further developers and cause issues in the long run when it comes down to shared development.

    Simple example:

    $Database = new Database(null,"root","pass","database");
    $Results = $Database->fetch_all("SELECT * from tabe_that_dont_exists");
    if($Results === false)
    {
         echo $Database->get_error();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Something i've never really done before, but what is the best way to make
Something like var life= { users : { guys : function(){ this.SOMETHING.mameAndDestroy(this.girls); }, girls
Something else perhaps? I am already using nHibernate, but I get occasional issues where
Something is eluding me ... it seems obvious, but I can't quite figure it
I have this working script (i know its incomplete): function rsvpNew(selector,function_url) { $(selector).livequery('click',function(){ var
gcc lovingly throws me this error: bst.c:33: error: invalid application of ‘sizeof’ to incomplete
I have been investigating how My ASP.NET MVC application can log unsuccessful / incomplete
I need to solve the following question which i can't get to work by
Something I have always been interested in out of curiosity, is there a tool
Something that has always bugged me is how unpredictable the setTimeout() method in Javascript

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.