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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:46:51+00:00 2026-06-11T09:46:51+00:00

I need help in understanding the right way to pass an object (MySQL connection)

  • 0

I need help in understanding the right way to pass an object (MySQL connection) created in one method to another method within the same class.

I am trying to create a database class that loads the connection using a constructor and all other methods, use this connection to perform their respective functions.

While trying to do so I keep getting this error:

Fatal error: Call to a member function query() on a non-object in /database.php on line 44

My code:

class database
{ 
    public $mysqli;

    public function __construct($database_server, $database_name, $database_user, $database_pass)
    {
        $mysqli = new mysqli($database_server,$database_user, $database_pass, $database_name);

        if ($mysqli->connect_error)
        {
                  die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
        }
        echo "Database connection established successfully.<br>";
        return $this->mysqli;

    }

    public function fetch_data()
    {

        $query = "select * from payment";
        if ($result = $this->mysqli->query($query))
        {

            // fetch associative array
            while ($row = $result->fetch_assoc())
            {
                printf ("%s (%s)\n", $row["id"], $row["status"]);
            }

        }

    }
}

Any help would be appreciated. Thank you everyone.

  • 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-11T09:46:52+00:00Added an answer on June 11, 2026 at 9:46 am
    public function __construct($database_server, $database_name, $database_user, $database_pass)
    {
        $mysqli = new mysqli($database_server,$database_user, $database_pass, $database_name);
    
        if ($mysqli->connect_error)
        {
                  die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
        }
        echo "Database connection established successfully.<br>";
    
        // instead of return, just set the local variable to the `mysqli` property.
        $this->mysqli = $mysqli;
    
    }
    

    Or

    public function __construct($database_server, $database_name, $database_user, $database_pass)
    {
        $this->mysqli = new mysqli($database_server,$database_user, $database_pass, $database_name);
    
        if ($this->mysqli->connect_error)
        {
           die('Connect Error (' . $this^>mysqli->connect_errno . ') '. $this->mysqli->connect_error);
        }
        echo "Database connection established successfully.<br>";    
    }
    

    And public $mysqli; will be better to be protected.

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

Sidebar

Related Questions

Need some help understanding if I'm on the right track here and exactly what
I think I need a little help understanding how a simple Rails 3.2 one-to-one
I need help understanding whats going on in the function, especially the return statements.
I need help understanding how to manage the API for an iPhone application that
I need help understanding how to demonize a process in Go. package main import
Kind of need help understanding what this code actually outputs. Does it out put
I'm fixing bugs for some application, and I need help understanding the following lines
I need some help understanding this bit of code pre { white-space: pre; white-space:
OK, I need some help understanding the process behind Facebook's website integration process and
Oracle's PL/SQL is fairly new to me, so I need some help understanding if

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.