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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:58:03+00:00 2026-06-06T19:58:03+00:00

I just started doing OOP, so I apologize in advance if there is a

  • 0

I just started doing OOP, so I apologize in advance if there is a simple solution.
Basically I need to use my $mysqli object inside a class. I’ve seperated it into two files.

config2.php

class Config
{

    public $host = 'localhost';
    public $username = '****';
    public $password = '****';
    public $database = '****';

    function report_error($query)
    {
        $email = '*@hotmail.com';
        $subject = 'MySQL error.';
        $message = "IP: {$_SERVER['REMOTE_ADDR']} \n URL: http://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']} \n\n MySQL query: {$query} \n\n MySQL error: " . $mysqli->error();

        mail($email, $subject, $message);
        die('Oops, an error has occured. The administrator has been notified.');
    }

}

$config = new Config();
$mysqli = new mysqli($config->host, $config->username, $config->password, $config->database);

if($mysqli->connect_error)
    report_error($mysqli);

administration.php

require('includes/config2.php');

$mysqli->real_escape_string();   // Works out of scope.

class Account
{
    public $username = $mysqli->real_escape_string();   // Doesn't work in scope.
    public $password;

    function login()
    {

    }
}

Thanks for the help guys, I appreciate it :).

  • 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-06T19:58:05+00:00Added an answer on June 6, 2026 at 7:58 pm

    You should pass the object to the constructor of Account and save it as a private instance variable.

    Account is directly dependent on an instance of mysqli, so there is nothing wrong with making that clear by specifying it as a required parameter in the constructor. That is the only way you can make sure that whenever Account is used the mysqli object is there too. If you access it from a global state instead (by either having a static accessor or by directly accessing the global scope) you can never guarantee that it is really there.

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

Sidebar

Related Questions

I've just started with C++, and maybe there's something that I'm doing wrong here,
I have just started doing TDD (and basically testing in general so bear with
I just started with Android and I'm doing a simple app that I want
I have just started doing webdesigning I use photoshop and dreamweaver. I have seen
I just started programming in objective-c(today) and I'm just doing some simple command line
I have just started to use Github and have been doing some tests with
I've just started doing some real-world performance testing on my Fluent NHibernate / SQLite
I just started working with play, and I modified the way I'm doing a
I'm doing a .NET unit as part of my studies. I've only just started,
Correct me If I am doing wrong, I have just started to check WCF

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.