connect = mysql_connect($server, $user, $password, true); return $connect; } function useDB($database) { $this->database = $database; return mysql_select_db($database, $this->connect); } function exec($query) { $obj = new DBQuery($query); $obj->exec(); $this->results[] = $obj; return $obj->results(); } function results($qid = NULL) { if($qid != NULL) { return $this->results[$qid]; } else { return $this->results[ count($this->results) - 1 ]->results(); } } } class DBQuery { private $results; private $query; function DBQuery($sql) { $this->query = $sql; $this->results = array(); } function exec() { $result = mysql_query($this->query); if($result && $result !== false && $result !== true) { while($row = mysql_fetch_assoc($result)) { foreach($row as $key => $value) { $row[$key] = stripslashes($value); } $this->results[] = $row; } } else { $this->results = NULL; } } function results($index = NULL) { return $this->results; } } function query($sql) { global $DBO; return $DBO->exec($sql); } function sqlify($str) { return mysql_real_escape_string(trim($str)); } /** * Generates an insert sql query from the parameters * * @param string $table The name of the table * @param array $array An associative array with the values similar to column=>value * @return string The sql query */ function getInsertSQL($table, $array) { $sql = 'INSERT INTO '.$table; $columns = ''; $values = ''; foreach($array as $key => $value) { $columns .= '`'.$key.'`, '; if($value != "") { $values .= "'".sqlify($value)."', "; } else { $values .= "NULL, "; } } $columns = substr($columns, 0, -2); $values = substr($values, 0, -2); $sql .= "($columns) VALUES ($values)"; return $sql; } /** * Generates an update sql query from the parameters * * @param string $table The name of the table * @param array $array An associative array with the values similar to column=>value * @param string $where What to limit the update to. Cannot be blank. * @return string The sql query */ function getUpdateSQL($table, $array, $where) { if(trim($where) == "") { return; } $sql = 'UPDATE '.$table.' SET '; foreach($array as $key => $value) { if($value != "") { $sql .= '`'.$key."`='".sqlify($value)."', "; } else { $sql .= '`'.$key."`=NULL, "; } } $sql = substr($sql, 0, -2); $sql .= " WHERE ".$where; return $sql; } function getDeleteSQL($table, $where) { if(trim($where) == "") { return; } return "DELETE FROM $table WHERE $where"; } ?>