<?php
$ip = "";
$user = "";
$pass = "";
$dbname = "";
$connection = mysql_connect($ip, $user, $pass) or die("Could not connect : " . mysql_error());
mysql_select_db($dbname, $connection);
//%a - automatically guess the data type
//%n - number
//%s - string
//%t - timestamp
class Query {
private static $columns = array();
$query;
$executed = false;
$result = NULL;
$time = NULL;
function __construct($query,$args=NULL) {
$offset = 0;
$converted = 0;
$new_query = '';
if(!is_null($args)) {
while($converted < sizeof($args)) {
$length = strlen($query);
$pos = strpos($query,'%',$offset);
if($pos === false) {
throw new Exception('Query: not all arguments converted.');
}
if($pos+1 >= $length) {
break;
}
$type = $query[$pos+1];
if($type == '%') {
continue;
}
$arg = $args[$converted++];
if($type == 'a') {
if(is_numeric($arg)) {
$type = 'n';
}
else {
$type = 's';
}
}
switch($type) {
case 'n':
if(!is_numeric($arg)) {
throw new Exception('Query: Argument is not numeric ('.$arg.').');
}
break;
case 's':
$arg = "'".mysql_real_escape_string($arg)."'";
break;
case 't':
if(!is_numeric($arg)) {
if(!($arg2 = strtotime($arg))) {
throw new Exception('Query: Unable to convert to timestamp ('.$arg.').');
}
$arg = $arg2;
}
$arg = 'from_unixtime('.$arg.')';
break;
default:
throw new Exception('Query: Unrecognized specifier ('.$type.').');
}
$new_query .= substr($query,$offset,$pos-$offset).$arg;
$offset = $pos+2;
}
}
$new_query .= substr($query,$offset);
$this->query = $new_query;
}
public function __toString() {
return $this->query;
}
public function execute() {
$start = microtime(true);
$this->result = mysql_query($this->query);
$this->time = microtime(true) - $start;
$this->executed = true;
return $this->result;
}
public function execution_time() {
return $this->time;
}
public function fetch_assoc() {
if(!$this->executed) {
$this->execute();
}
return mysql_fetch_assoc($this->result);
}
public function num_rows() {
if(!$this->executed) {
$this->execute();
}
return mysql_num_rows($this->result);
}
public function affected_rows() {
if(!$this->executed) {
$this->execute();
}
return mysql_affected_rows();
}
public static function escape_string($input) {
return mysql_real_escape_string($input);
}
public static function columns($table) {
if(!array_key_exists($table,self::$columns)) {
$query = new Query("show columns from `".Query::escape_string($table)."`");
self::$columns[$table] = array();
while($row = $query->fetch_assoc()) {
self::$columns[$table][$row['Field']] = $row;
}
unset($query);
}
return self::$columns[$table];
}
}
?>
So for this code, I get a Parse error: syntax error, unexpected T_VARIABLE on lines 19-22. Not quite sure why. I have checked through it a couple times and I don't believe I see anything wrong.
Any Help?