Skip to content

Instantly share code, notes, and snippets.

@bkilshaw
Created May 5, 2011 21:42
Show Gist options
  • Save bkilshaw/958030 to your computer and use it in GitHub Desktop.
Save bkilshaw/958030 to your computer and use it in GitHub Desktop.
<?php
class SQLQuery {
public $query = "";
private $allowed_query_types = array(
'insert'
, 'delete'
, 'update'
, 'select'
);
public function __construct($query) {
$this->query = trim($query);
$query_type = $this->query_type();
if(in_array($query_type, $this->allowed_query_types)) {
return $this->$query_type();
} else {
return FALSE;
}
}
private function query_type() {
$query_parts = explode(" ", $this->query);
$query_type = strtolower($query_parts[0]);
return $query_type;
}
public function insert() {
$result = mysql_query($this->query);
if($result) {
return mysql_insert_id();
} else {
return FALSE;
}
}
public function select() {
$return_array = array();
$result = mysql_query($this->query);
if($result) {
if(mysql_num_rows($result) == 0) {
return FALSE;
} else {
while ($row = mysql_fetch_assoc($result)) {
$return_array[] = $row;
}
return $return_array;
}
} else {
return FALSE;
}
}
public function delete() {
return mysql_query($this->query);
}
public function update() {
return mysql_query($this->query);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment