[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.
requireis not actually a function, it's a language construct - and these are also case-insensitive.require 'db.class.php';