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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:03:28+00:00 2026-06-07T16:03:28+00:00

[Edited/Updated] Allow me to explain the situation. I have three files: Below is db.class.php.

  • 0

[Edited/Updated]

Allow me to explain the situation. I have three files:

Below is db.class.php. When called, it connects to the DB in the constructor and closes the connection in the destructor. If you notice the query() method, it’s simply a static INSERT at the moment, as that is where I’m stuck.

<?php
//
// Class: db.class.php
// Desc: Connects to a database and runs PDO queries via MySQL
// Settings:
error_reporting(E_ALL);
////////////////////////////////////////////////////////////
class Db
{
    # Class properties
    private $DBH; // Database Handle
    private $STH; // Statement Handle

    # Func: __construct()
    # Desc: Connects to DB
    public function __construct()
    {
        // Connection information
        $host   = 'localhost';
        $dbname = 'removed';
        $user   = 'removed';
        $pass   = 'removed';

        // Attempt DB connection
        try
        {
            $this->DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
            $this->DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            echo 'Successfully connected to the database!';
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }

    # Func: query(sql statement)
    # Desc: Sends a query to the DB
    public function query($sql_statement)
    {
        $sql = array(':color' => $sql_statement);
        $this->STH = $this->DBH->prepare("INSERT INTO color_table (color) value ( :color )");
        $this->STH->execute($sql);
    }

    # Func: __destruct()
    # Desc: Disconnects from the DB
    public function __destruct()
    {
        // Disconnect from DB
        $this->DBH = null;
        echo 'Successfully disconnected from the database!';
    }
}
?>

Below is colors.class.php. For now, it only has an insert function. What I’m trying to do is essentially rip the what I have in the query() function from db.class.php and put it into the insertColor() function here, and pass the entire SQL statement whether it’s an insert, delete, or update to the query() function.

<?php
//
// Class: colors.class.php
// Desc: Provides methods to create a query to insert,
//       update, or delete a color from the database.
////////////////////////////////////////////////////////////
class Colors
{
    # Class properties
    protected $db;

    # Func: __construct()
    # Desc: Passes the Db object so we can send queries
    public function __construct(Db $db)
    {
        $this->db = $db;
    }

    # Func: insertColor()
    # Desc: Sends an INSERT querystring
    public function insertColor($color)
    {
        $this->db->query($color);
        echo 'Inserted color:' . $color;
    }
}
?>

Below we have colors.php which is where everything above is instantiated and implemented. So here is where I’d pass what color I actually want to be inserted into the database.

<?php
Require('db.class.php');
Require('colors.class.php');

$db = new Db;
$colors = new Colors($db);
$colors->insertColor('TestColor'); // Passing the color here to be put into an insert statement

?>

Where I’m essentially stuck is I’m trying to make the colors.class.php create a PDO statement, and then pass it to the db.class.php query() method when they’re ready to run. Right now, the PDO statement is simply defined in the query() method but I’m trying to avoid this. Essentially, I want to rip what I have out of the query() method and split it into three methods in the Colors class, one for insert, update, and delete.

However, I’m rather new to OOP programming and am having issues with all of the syntax, nor to I know if this is even a good approach. Any help is greatly appreciated, and let me know if more details or information is needed.

  • 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-07T16:03:30+00:00Added an answer on June 7, 2026 at 4:03 pm

    Yeah, because $db is a local variable inside the method. Pass it as an argument:

    class Colors
    {
      protected $db;
    
      public function __construct(Db $db = null)
      {
        $this->db = $db;
      }
    
      public function insertColor()
      {
        if ($this->db != null)
        {
          $this->db->query();
        }
      }
    }
    

    And query is a method so you have to add the brackets: $db->query()

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

Sidebar

Related Questions

I am planning to allow users to generate .POT files/.PO files through a PHP
I have a series of XML files which can be retrieved, edited and saved
Edited Question: This should be clear. using System; namespace UpdateDateTimeFields { class Program {
I used 'package.skeleton()' to generate .Rd help files a few months ago. I have
I would like to implement a JTable which will only be edited and updated
I have database called Auth, there are fields: id, auth, is_active, created_at, updated_at i
I have a Web service which, when updated on one computer with VS2008 works
I've seen Veloedit , which seems to have good syntax highlighting but doesn't allow
The setup: I have a standard .php file (index.php) that contains two includes, one
UPDATE: Edited javascript code. It's only slightly off now in some columns. Maybe a

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.