OK... here's what I have so far:
Code: Select all
class DataBase{
function DataBase($host="localhost", $user="root", $pass="pass", $dbas="pdirect_directory"){
//Make mysql connection
if(mysql_connect($host, $user, $pass)){
//select database to work with
if(!mysql_select_db($dbas)){
//if we can't select the database, we log the error
logerror("Could not select database - " . $dbas . "\n MySQL says: " . mysql_error());
}
}
else{
//if we can't connect, log the error
logerror("Could not to " . $host . " connect with user - " . $user . "\n MySQL says: " . mysql_error());
}
}
function AddTable($name, $columns, $auto=1){
//method accepts the name of the table to be created as first argument - the table name and _ is appended to the beginning of each column name
//also accepts an associative array of columns as the second. Keys are column names, values are data types of those columns (put value lengths
//in parenthesis next to data type) like this: $array['columnname'] = "VARCHAR(123)";
//you do not need to make an auto increment id column
//the third argument is set to one if you want an id column to be automatically inserted as an auto increment. Set to 0 if you don't want this
//returns true if successful
//start the query by creating a table and name it the specified name
$sql = "CREATE TABLE `" . $name . "` (";
if($auto == 1){
//if argument three is set to 1, this will add an auto-increment primary key id
$id = $name . "_" . "id";
$sql .= "`" . $id . "` INT NOT NULL AUTO_INCREMENT , PRIMARY KEY ( `" . $id . "` ), ";
}
foreach($columns as $key => $val){
//this will prefix all column names with the table name and add them to the query
$sql .= "`" . $name . "_" . $key . "` " . $val . ",";
}
//remove the comma
$sql = substr($sql, 0, -1);
//add the end parenthesis
$sql .= ");";
if(mysql_query($sql)){
return true;
}
die($sql);
logerror("Could not execute query: " . $sql . "\n MySQL says: " . mysql_error());
return false;
}
function DropTable($name){
//this method accepts a table name to be dropped. returns true if successful
$sql = "DROP TABLE " . $name . ";";
if(mysql_query($sql)){
return true;
}
logerror("Could not execute query: " . $sql . "\n MySQL says: " . mysql_error());
return false;
}
function Insert($values, $table){
//method accepts the table name as second argument, and the values to be inserted into that table as the first
//returns true if successful
//start query with specified table
$sql = "INSERT INTO " . $table . " VALUES ( ";
foreach($values as $val){
//add each escaped value of the array argument to the sql query
$sql .= " '" . mysql_real_escape_string($val) . "',";
}
//remove the comma from the end of the sql query
$sql = substr($sql, 0, -1);
//add end parenthesis to sql query
$sql .= ");";
if(mysql_query($sql)){
return true;
}
logerror("Could not execute query: " . $sql . "\n MySQL says: " . mysql_error());
return false;
}
function Delete($column, $values, $table){
//method accepts the name of the table we are working with as well as an array of the values to be deleted.
//method also accepts a column name to search for these values in
//returns true if successful
//start query with specified table and column
$sql = "DELETE FROM " . $table . " WHERE " . $column . " IN (";
foreach($values as $val){
//add each value to be deleted to the query
$sql .= " '" . $val . "',";
}
//remove the comma from the end of the sql query
$sql = substr($sql, 0, -1);
//add end parenthesis to sql query
$sql .= ");";
if(mysql_query($sql)){
return true;
}
logerror("Could not execute query: " . $sql . "\n MySQL says: " . mysql_error());
return false;
}
}
So I have the constructor which opens the connection and then AddTable, DropTable, Insert, and Delete methods. I know I still need a select method, but is there any other type of database interaction I am missing that is commonly needed?
The delete method is pretty cool... it can delete based on any column and can delete multiple rows... pretty cool.