Click to See Complete Forum and Search --> : Database Simple Queryies OO


KingWylim
02-06-2008, 04:11 PM
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);
}
}
?>

NogDog
02-06-2008, 10:55 PM
Well, I don't think that would even parse, since you did not prepend your class variables with a "$", and also you can only assign default values to class variables via constants or literal values, you cannot assign variables or function/method results. Therefore you will need to move the assignment of the value for those last two class variables into the constructor method.

Also, I'm a bit concerned to see you are using mysql_pconnect() instead of mysql_connect(). It's not to say that you should not ever use persistent connections, but if you do not have a specific reason for doing so and are not aware of the potential side-effects, I would advise against it as you may find yourself eating up all available database connections and essentially breaking the application.