// Developed by Francesco `ascii` Ongaro (http://www.ush.it/) 31 May 2010 class mysqli_ng { private $link; private $stmt; private $stmt_buffer; public function __construct($host, $user, $pass, $db) { $this->link = new mysqli($host, $user, $pass, $db); if ($this->link->connect_error) trigger_error( 'Connect Error ('.$this->link->connect_errno.') '.$this->link->connect_error, E_USER_ERROR ); } public function affected_rows() { return $this->link->affected_rows; } public function stmt($template, $bindings = NULL) { if ($this->stmt = $this->link->prepare($template)) { // Binding the inputs if ($bindings !== NULL) { $types = ''; $values = array(); foreach ($bindings as $binding) { $types .= $binding[0]; $values[] = $binding[1]; } $fields[] = $types; foreach ($values as $value) $fields[] = &$value; call_user_func_array(array($this->stmt, 'bind_param'), $fields); } // Executing the prepared statement $this->stmt->execute(); // Binding the results $data = $this->stmt->result_metadata(); $this->stmt_buffer = array(); $fields = array(); while($field = $data->fetch_field()) { $fields[] = &$this->stmt_buffer[$field->name]; } call_user_func_array(array($this->stmt, 'bind_result'), $fields); } } function stmt_fetch() { if ($this->stmt->fetch()) return $this->stmt_buffer; return FALSE; } function stmt_close() { $this->stmt->close(); } function close() { $this->link->close(); } } $db = new mysqli_ng('host', 'user', 'pwd', 'database'); $db->stmt( 'SELECT * FROM `users` WHERE `id` = ?;', array(array('i', '1')) ); while (FALSE !== $row = $db->stmt_fetch()) { print_r($row); } $db->stmt_close(); $db->close();