diff --git a/src/Statement.php b/src/Statement.php index 0286b8e..e18280f 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -11,22 +11,42 @@ class Statement private $params; private $result; private $stored; + private $error; - public function __construct(Environment $environment, $query) + public function __construct(Environment $environment, $query = null) { $this->environment = $environment; $this->prepare($query); } + public function error() + { + return $this->error; + } + + private function set_error($error) + { + $this->error = $error; + return false; + } + public function bind_param($types, ...$params) { - $this->params = []; + $length = strlen($types); + if($this->query === null) { + return $this->set_error("Unable to perform a bind_param without a prepare"); + } if($length != count($params)) { + return $this->set_error("bind_param's number of types in the string doesn't match number of parameters passed in"); + } + $param = current($params); - $length = strlen($types); for($i = 0; $i < $length; ++$i, $param = next($params)) { $type = $types[$i]; - switch($type) { + if($type == null) { + $this->params[] = 'NULL'; + } else { + switch($type) { case 'i': $this->params[] = (int) $param; break; @@ -37,13 +57,19 @@ public function bind_param($types, ...$params) case 'o': $this->params[] = "'". addslashes((string) $param) . "'"; break; + } } } + return true; } public function execute() { + if($this->query === null) { + return $this->set_error("Unable to perform an execute without a prepare"); + } + $that = $this; $count = 0; $newQuery = preg_replace_callback( @@ -53,16 +79,17 @@ function($match) use ($that, &$count) { return $that->params[$count++]; }, ); $result = $this->environment->query($newQuery); if($result instanceof ResultSet) { - $this->result = $result; - return true; + $this->result = $result; + return true; } else { - return $result; + return $result; } } public function prepare($query) { $this->query = $query; + $this->params = []; $this->result = null; $this->stored = false; return true; @@ -70,12 +97,30 @@ public function prepare($query) public function store_result() { + if($this->query === null) { + return $this->set_error("Unable to perform a store_result without a prepare"); + } + $this->stored = true; + return true; + } + + public function get_result() + { + if($this->query === null) { + return $this->set_error("Unable to perform a get_result without a prepare"); + } + + return $this->result; } public function result_metadata() { - return $this->result; + if($this->query === null) { + return $this->set_error("Unable to perform a result_metadata without a prepare"); + } + + return $this->result; } public function free_result() @@ -83,5 +128,4 @@ public function free_result() } - }