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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:17:09+00:00 2026-05-26T17:17:09+00:00

I am working on something. I am trying to Create a class in config.php

  • 0

I am working on something. I am trying to Create a class in config.php that contains a method to connect to the database.In dbConnectExample.php, instantiate that class and call the method. I am kinda getting confused because I am learning to others programming classes too. This is what I have so far;

    <?php # Scripts 6.3 - Rectangle.php

Class Rectangle {

//Declare the attributes:
public $width = 0;
public $height = 0;

//Method to set the dimensions.
Function set_size($w = 0, $h = 0) {
        $this->width = $w;
        $this->height = $h;
}

//Method to calculate and return the area.
function get_area() {
    return ($this->width * $this->height);
    }

// Method to calculate and return the perimeter.
function get_perimeter() {
    return ( ( $this->width + $this->height) * 2 );
    }

    //Method to determine if the rectangle
    //is also a square
    function is_square() {

    if ($this->width == $this->height) {
    return true; //Square
    } else {
        return false; //Not a square
    }
  }

} //End of Rectangle class.

I know I am creating a class with a method. I am just confused to connect to a database you go like this;

    //Connect to the database
$DBConnect = @new mysqli("localhost", "valerie_2shuawna", "norris");
if (mysqli_connect_errno())
      die("<p>Unable to connect to the database server.</p>"
      . "<p>Error code " . mysqli_connect_errno()
      . ": " . mysqli_connect_error()) . "</p>";
$DBName = "config.php";
    @$DBConnect->select_db($DBName)
         Or die("<p>Unable to select the database.</p>"
         . "<p>Error code " . mysqli_errno($DBConnect)
         . ": " . mysqli_error($DBConnect)) . "</p>";

$DBConnect->close();

But I am suppose to instantiate that class and call the method. and then the config it is suppose to contain a method to connect to the database. If someone can help me figure out what I am doing wrong and maybe explain so I am not so confused I would be grateful 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-05-26T17:17:09+00:00Added an answer on May 26, 2026 at 5:17 pm

    You can create a simple class:

    // Database.php
    
    class Database
    {
        public static function getDbo($dbName)
        {
            $dbo = new mysqli("localhost", "user", "password");
    
            if (mysqli_connect_errno()) {
                throw new Exception(
                    "<p>Unable to connect to the database server.</p>"
                   . "<p>Error code " . mysqli_connect_errno()
                   . ": " . mysqli_connect_error()) . "</p>"
                );
            }
    
            if (!$dbo->select_db($dbName)) {
                throw new Exception(
                    "<p>Unable to select the database.</p>"
                    . "<p>Error code " . mysqli_errno($DBConnect)
                    . ": " . mysqli_error($DBConnect)) . "</p>"
                );
            }
    
            return $dbo;
        }
    }
    
    // SomeFile.php
    
    require_once 'Database.php';
    
    try {
        $dbo = Database::getDbo();
    
        // If I made it this far then I can start querying
        $dbo->query('SELECT stuff FROM table');
    
        // etc.
    
    } catch (Exception $e) {
        die($e->getMessage());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a class that lets me call methods with a
I'm working on something that was built on a PC set-up using php and
I'm trying to create a form that will animate something while processing a particular
What i'm trying to do is create a template array class that will store
I'm working on a little something and I am trying to figure out whether
I'm trying to get this simple PowerShell script working, but I think something is
I'm working on something that requires traversing through the file system and for any
I'm working on something that pulls in urls from delicious and then uses those
I'm currently trying to create a ControlTemplate for the Button class in WPF, replacing
I'm trying to use Ruby's vector class in a game I'm working on, but

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.