- Joined
- Jun 2, 2012
- Messages
- 765
- Reaction score
- 294
Wasn't going to show this off, but I started to wonder if I could improve it.
It works like a charm, just wanted to know what other people thought.
It works like a charm, just wanted to know what other people thought.
PHP:
class DatabasePreparedStatement
{
private $connection, $statement, $numrows, $fetch, $insertid, $insert = false;
public $results = array();
public function SetConnection($connection)
{
$this->connection = $connection;
return $this;
}
public function Prepare($query)
{
if(!$this->statement = $this->connection->prepare($query))
{
$this->__Error($this->connection->error);
}
return $this;
}
public function Bind($data)
{
$types = "";
foreach($data as $key => $value)
{
$types .= $this->__Type($value);
}
$params = array($types);
foreach($data as $key => $value)
{
$params[] =& $data[$key];
}
call_user_func_array(array($this->statement, "bind_param"), $params);
return $this;
}
public function Results($data)
{
$results = array();
foreach($data as $key)
{
$results[$key] = "";
}
call_user_func_array(array($this->statement, "bind_result"), &$results);
$this->results = $results;
return $this;
}
public function Execute()
{
if(!$this->statement->execute())
{
$this->__Error($this->statement->error);
}
return $this;
}
public function Store()
{
if(!$this->statement->store_result())
{
$this->__Error($this->statement->error);
}
return $this;
}
public function NumRows()
{
$this->numrows = $this->statement->num_rows;
return $this;
}
public function Fetch($while = false)
{
if($while != true)
{
$this->statement->fetch();
return $this;
}
else
{
return $this->statement->fetch();
}
}
public function InsertId($insert = false)
{
$this->insert = $insert;
$this->insertid = $this->statement->insert_id;
return $this;
}
public function Close()
{
if(!$this->statement->close())
{
$this->__Error($this->statement->error);
}
return $this;
}
private function __Error($string)
{
$error = new CoreError;
die($error->SetError($string)->Call());
}
private function __Type($var)
{
return substr(gettype($var), 0, 1);
}
public function Call()
{
if($this->insert != false)
{
return $this->insertid;
}
elseif($this->results)
{
return $this->results;
}
elseif($this->numrows > 0)
{
return $this->numrows;
}
elseif($this->numrows < 1)
{
return 0;
}
else
{
return false;
}
}
}