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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:39:46+00:00 2026-05-26T05:39:46+00:00

<?php class UBC_DB { private $db; public function connect() { $db = new mysqli(‘localhost’,

  • 0
<?php
  class UBC_DB
  {
      private $db;


   public function connect()
   {
       $db = new mysqli('localhost', 'root', 'root', 'NewsTable');
   }

   public function getDB()
   {
       if(!$db)
       {
           printf("Can't connect to MySQL Server. ErrorCode: %s\n", mysqli_connect_error());
           exit;
       }
   }
}

$api = new UBC_DB();
$api->connect();
$api->getDB();
?>

Hello, PHP masters.
I’ve got a problem here and need your help…
I’m trying to make a nice neat class to deal with DB connection… However,
even if that db is connected successfully and returns the appropriate result to $db, I cannot reuse this variable in another method in the same class! shouldn’t $db remember what it has received before? In getDB method, it says $db has nothing : ( PHP has a different variable scope-rules?

  • 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-26T05:39:47+00:00Added an answer on May 26, 2026 at 5:39 am

    The scoping rules are different than other languages like Perl, it is true.

    I suggest the following singelton-style DB class:

    <?php
      class UBC_DB
      {
          private static $db;
    
    
       private static function connect()
       {      
           self::$db = new mysqli('localhost', 'root', 'root', 'NewsTable');       
       }
    
       public static function getDB()
       {
           if(!self::$db)
           {
               self::connect();
           }
           return self::$db;
       }
    }
    
    
    $db = UBC_DB::getDB();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one PHP class thus: class DB extends mysqli{ public function __construct( {
My PHP class looks like that. <?php class htmlize { protected $base_dir; public function
class_1.php class class_1 { public function __construct() { require_once $_SERVER['DOCUMENT_ROOT'].'/array.php'; } } class_2.php class
I have following php class class User implements IUser { public function __construct($i_objParent =
Is there any way to call a php class (eg. $var = new className)
Consider the following PHP class code class SuperIdea{ . . . static function getById($id){
I want simple PHP class or function to return the URL of the FLV
for example in PHP class foo{ function foo($name){ //constructor $this->name=$name; } function sayMyName(){ return
from http://www.php.net/manual/en/class.pdo.php ###### config.ini ###### db_driver=mysql db_user=root db_password=924892xp [dsn] host=localhost port=3306 dbname=localhost [db_options] PDO::MYSQL_ATTR_INIT_COMMAND=set
from http://www.php.net/manual/en/class.pdo.php ###### config.ini ###### db_driver=mysql db_user=root db_password=924892xp [dsn] host=localhost port=3306 dbname=localhost [db_options] PDO::MYSQL_ATTR_INIT_COMMAND=set

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.