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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:31:06+00:00 2026-06-17T21:31:06+00:00

I am new to using PHP in an OOP way but have found an

  • 0

I am new to using PHP in an OOP way but have found an issue with my database connection class.

I have a file with this mysqli connection class here

$db_name = 'dbname';
$db_user = 'dbuser';        
$db_password = 'dbpassword';
$db_host = 'localhost';

class database {

    public $mysqli;

    public function connect($db_host, $db_user, $db_password, $db_name){

        $this->mysqli = new mysqli($db_host, $db_user, $db_password, $db_name);

        if ($mysqli->connect_errno) {
            return "Sorry Andre, but you seem to have messed up the DB connection :(";
        }
    }
}

$newConnection = new database;
$newConnection->connect($db_host, $db_user, $db_password, $db_name);

I then want to use the variable $mysqli in a db connection in another file – this is a simple insert to a database using $mysqli variable to connect. I included the above in the connection file but it seems the $mysqli variable is not being returned when I call the method within the database class. I get the PHP error saying…

Fatal error: Call to a member function prepare() on a non-object in...

I have seen that using

global $mysqli; 

works however I want to do it the proper way as I have heard that is not good practice.

I understand that I am potentially doing things a bit wrong here as I’m new to using OOP but I assumed that by returning that variable in the connect function I can then access it from creating the class outside.

Help is appreciated,
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-06-17T21:31:08+00:00Added an answer on June 17, 2026 at 9:31 pm

    When using it outside , you access a class variable via the instance…

    $newConnection = new database;
    $newConnection->connect($db_host, $db_user, $db_password, $db_name);
    $newConnection->mysqli /* here you have access from outside */
    

    from inside you use the keyword $this…

    // like this from inside
    if ($this->mysqli->connect_errno) {
        return "Sorry Andre, but you seem to have messed up the DB connection :(";
    }
    

    In case you want to protect your variable from outside access use:

      private $mysqli; 
      // instead of 
      public $mysqli;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to PHP oop stuff. I'm trying to create class database and call
Righty-o I am fairly new to OOP. Im using PHP v5+ I have my
Not completely new to PHP itself, but new to using OOP w/ PHP. Object
I'm newish to PHP and OOP and I have been using the PDO class
I've got a new site setup using CMSMadeSimple (PHP based) but I'm having problems
i have made a server using php. The server is getting new information after
I have a PHP script where I create a new process using proc_open().It executes
I am using PDO with PHP to create a new database and then a
I'm relatively new using OOP in PHP. It's helped immensely in the organization and
I'm new to OOP using PHP and the idea seems a little pointless in

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.