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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:18:18+00:00 2026-06-14T09:18:18+00:00

I am new to PHP OOP. Below is my VERY FIRST class file. I

  • 0

I am new to PHP OOP. Below is my VERY FIRST class file. I would like to add more flexibility to this code, add functions so I can run queries and even assign the results (either fetch_assoc/fetch_array) to an array (Or var, etc) for latter use.

The problem I am having with running queries, is that I am not able to put together both classes (nesting?): $db->Query->Select('myTable'); OR $db->Query->Select('myTable')->Where($pageID); OR $db->Query->Select('myTable')->Where($pageID)->OrderBy('name');

ALSO, I would appreciate if you let me know if there’s something I am doing wrong in this code AND suggestions for improvements, so I can write better php classes in the future =).

<?php
require( $_SERVER['DOCUMENT_ROOT'] . '/database/database-connection-info.php');
 //This file (database-connection-info.php) contains $config[];

class db {
    private $db;
    private $type = 'read-only';
    //$config['db']['dbh']          = Database Host
    //$config['db']['dbn']          = Database Name
    //$config['db'][$type]['dbu']   = [User type][user name]
    //$config['db'][$type]['dbp']   = [User type][password]


    public function __construct($type = null) {
        global $config;
        $this->config = $config;
        $this->Connect($type);
    }


    public function Connect($type) {
    global $config;
        switch( $type ) {
        case 'admin':
        $this->type = 'admin';
        $this->db = mysql_connect( $this->config['db']['dbh'] , $this->config['db'][$type]['dbu'] , $this->config['db'][$type]['dbp'] );
        return $this->db;

        default:
        $this->type = 'read-only';
        $type = 'read';
        $this->db = mysql_connect( $this->config['db']['dbh'] , $this->config['db'][$type]['dbu'] , $this->config['db'][$type]['dbp'] );
        return $this->db;
        }
    }


    public function Close() {
        mysql_close();
        mysql_close($this->db);
        $this->type = 'Closed';
    }


    public function Type() {
        return "<b>Database connection type</b>: " . $this->type . "<br/>";
    }


    public function Status() {
        if( !@mysql_stat($this->db) )
             { return "<b>Database connection status</b>: There is no database connection open at this time.<br/>"; }
        else { return "<b>Database connection status</b>: " . mysql_stat($this->db) . "<br/>"; }
    }
}

//For testing purposes
$db = new db(admin);
echo $db->Type();
echo $db->Status();
echo $db->Close();
echo $db->Status();
echo $db->Type();
?>
  • 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-14T09:18:19+00:00Added an answer on June 14, 2026 at 9:18 am

    Two significant improvements:

    • Don’t use a global for the config, instead pass the config in the constructor. This allows you to instantiate db objects to other databases without having to hack a global.

    • Stop using the native [depreacted] mysql_* library and switch to a modern API such as PDO or MySQLi. This is a good PDO tutorial, especially in your case because you are coming from the native mysql_* perspective.

    I also noticed this:

    $db = new db(admin);
    

    PHP will look for a constant admin here, it will fail to find it and assume the string admin was intended. If you ever had a constant defined as admin then you would observe strange behavior there (or what you might think is string anyway). Change to a string:

    $db = new db('admin');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made an PHP OOP Cart Class like this print_r($_SESSION[cart]); the result is: Array
I am pretty new to OOP with PHP. OK first of all this produces
I think this is mostly because I'm new to PHP OOP, but I have
I'm new to PHP oop stuff. I'm trying to create class database and call
I'm a new guy in PHP OOP concepts. One of the first things that
I'm new to OOP in PHP, is that to seems correct ? class whatever
i have the oop php code: class a { // with properties and functions
I am very new to OOP styled PHP, and I am trying to implement
I'm learning php and OOP programming here. I have below working code I am
I am writing a validation class in PHP which I would like to be

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.