I'm new to OO so I'm not sure if this is the best way to do this if there is a way to optimize this I would appreciate it.
<?php
//Database class functions
class DB{
var hostname = "localhost"; //DB Host
var database = "main"; //DB Name
var username = "username"; //Username for DB
var password = "password"; //Password for DB
//Connects to the Database
var trustinmetcg = mysql_pconnect($this->hostname, $this->username, $this->password) or trigger_error(mysql_error(),E_USER_ERROR);
var select_db = mysql_select_db($this->database, $this->trustinmetcg); // Selects the Database;
//Constructor Function
function __construct($table_in, $fields_in, $field_val_in, $table_var_in, $where_in){
$this->table = $table_in; //Table Name
$this->fields = $fields_in; //Fields being updated, inserted, or selected
$this->field_val = $field_val_in; //Fields Values
$this->table_var = $table_var_in; //Table Row Name for WHERE Statement
$this->where = $where_in; //Table Valeu for WHERE Statement
}
//Update Database Record Function
function Update(){
$this->call = "UPDATE ".$this->table." SET ";
if(is_array($this->fields)){ //Checks to find out if there are multiple fields that need update
$this->total = count($this->fields); //Counts the number of fields
for($i=0; $i <= $this->total; $i++){
if(isset($this->set)){ //Decides whether to add to $this->set or to create the first record
$this->set = $this->set.","$this->fields[$i]."=".$this->field_val[$i]; //Adds to $this->set string.
} else {
$this->set = $this->fields[$i]."=".$this->field_val[$i]; //Creates first $this->set string.
}
}
} else {
$this->set = $this->fields ."=". $this->field_val; //Generates string if not working with arrays
}
$this->sql = $this->call."".$this->set." WHERE ".$this->table_var."="$this->where; //SQL Statement
$this->result = mysql_query($this->sql, $this->trustinmetcg)or die(mysql_error()); //Executes the query
$this->message = "Record's Updated Successfully."; //Returned message to User
return ($this->message); //Returing message.
mysql_free_result($this->trustinmetcg); //Clears the MYSQL Cache and closes the connection.
}
//Delete Database Record Function
function Delete(){
$this->sql = "DELETE FROM ".$this->table." WHERE ".$this->table_var."=".$this->where; //SQL Statement
$this->result = mysql_query($this->sql, $this->trustinmetcg)or die(mysql_error()); //Executed the query
$this->message = "Record's Deleted Successfully." //Returned message to User
return ($this->message); //Returning message returns 1 if true and 2 if false.
mysql_free_result($this->trustinmetcg); //Clears the MYSQL Cache and closes the connection
}
//Insert Database Record Function
function Insert(){
if(is_array($this->fields)){ //Checks to find out if there are multiple fields that need update
$this->total = count($this->fields); //Counts the number of fields
for($i=0; $i <= $this->total; $i++){
if(isset($this->set)){ //Decides whether to add to $this->set or to create the first record
$this->set[0] = $this->set.","$this->fields[$i]; //Adds to $this->set string.
$this->set[1] = $this->set.","$this->field_val[$i];
} else {
$this->set[0] = $this->fields[$i]; //Creates first $this->set string.
$this->set[1] = $this->field_val[$i]; //Creates first $this->set string
}
}
} else {
$this->set[0] = $this->fields; //Generates string if not working with arrays
}
$this->sql = "INSERT INTO ".$this->table."(".$this->set[0].") VALUES (".$this->set[1].")";
$this->result = mysql_query($this->sql, $this->trustinmetcg)or die(mysql_error()); //Executes the query
$this->message = "Record Added Successfully";
return($this->message);
mysql_free_result($this->trustinmetcg);
}
//Select Database Record/Records Function
function Select(){
if(is_array($this->fields)){ //Checks to find out if there are multiple fields that need update
$this->total = count($this->fields); //Counts the number of fields
for($i=0; $i <= $this->total; $i++){
if(isset($this->set)){ //Decides whether to add to $this->set or to create the first record
$this->set[0] = $this->set.","$this->fields[$i]; //Adds to $this->set string.
} else {
$this->set[0] = $this->fields[$i]; //Creates first $this->set string.
}
}
} else {
$this->set[0] = $this->fields; //Generates string if not working with arrays
}
$this->sql = "SELECT ".$this->set[0]." FROM ".$this->table." WHERE ".$this->table_var."=".$this->where;
$this->result = mysql_query($this->sql, $this->trustinmetcg)or die(mysql_error()); //Executes the query
return($this->result);
mysql_free_result($this->trustinmetcg);
}
}
?>