note 95871 deleted from mysqli-stmt.bind-param by danbrown

From: Date: Tue, 26 Jan 2010 22:26:00 +0000
Subject: note 95871 deleted from mysqli-stmt.bind-param by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-165338@lists.php.net to get a copy of this message
Note Submitter: Anonymous ---- <?php /** Had to strip all comments because this stupid site tells me it's too long... */ class DatabaseConnection { private $Connection = null; public function Connection() { return $this->Connection; } private function __construct() { $this->Connection = new mysqli(_DBHOST, _DBUSER, _DBPASS, _DBNAME); //catch any errors if (mysqli_connect_errno()) { throw new Exception(sprintf(_('MySQLi error %d: %s'), mysqli_connect_errno(), mysqli_connect_error())); } $this->Connection->set_charset("UTF8"); } private function stmt_bind_assoc (&$stmt, &$out) { $data = $stmt->result_metadata(); $fields = array(); $out = array(); $count = 0; while(($field = mysqli_fetch_field($data))) { $fields[$count] = &$out[$field->name]; $count++; } call_user_func_array(array($stmt,'bind_result'), $fields); } public function getRecords($qry) { $stmt = $this->Connection->prepare($qry); if ($stmt) { if (func_num_args()>=3) { $bind_names[] = func_get_arg(1); for ($i=2; $i<func_num_args();$i++) { $bind_name = 'bind' . $i; $$bind_name = func_get_arg($i); $bind_names[] = &$$bind_name; } call_user_func_array(array($stmt,'bind_param'),$bind_names); } $stmt->execute(); if ($stmt->errno==0) { $row = array(); $result = array(); $this->stmt_bind_assoc($stmt,$row); while ($stmt->fetch()) { array_push($result,$row); $this->stmt_bind_assoc($stmt,$row); } $stmt->close(); return $result; } else { $error = sprintf(_('Error executing query (%d): %s'),$stmt->errno,$stmt->error); $stmt->close(); throw new Exception($error); } } else { throw new Exception(sprintf(_('MySQLi error %d: %s'), mysqli_errno($this->Connection), mysqli_error($this->Connection))); } } public function executeQuery($qry) { $stmt = $this->Connection->prepare($qry); if ($stmt) { if (func_num_args()>=3) { $bind_names[] = func_get_arg(1); for ($i=2; $i<func_num_args();$i++) { $bind_name = 'bind' . $i; $$bind_name = func_get_arg($i); $bind_names[] = &$$bind_name; } call_user_func_array(array($stmt,'bind_param'),$bind_names); } $stmt->execute(); if ($stmt->errno==0) { if (strtolower(substr(trim($qry),0,6))=='insert') { //For insert queries, we return the insert_id $result = $stmt->insert_id; } else { //For update/delete queries we return the number of affected rows $result = $stmt->affected_rows; } $stmt->close(); return $result; } else { $error = sprintf(_('Error executing query (%d): %s'),$stmt->errno,$stmt->error); $stmt->close(); throw new Exception($error); } } else { throw new Exception(sprintf(_('MySQLi error %d: %s'), mysqli_errno($this->Connection), mysqli_error($this->Connection))); } } public function BeginTransaction() { $this->Connection()->autocommit(FALSE); } public function CommitTransaction() { $this->Connection()->commit(); $this->Connection()->autocommit(TRUE); } public function RollbackTransaction() { $this->Connection()->rollback(); $this->Connection()->autocommit(TRUE); } public static function GetInstance() { static $DatabaseConnection = null; if ($DatabaseConnection == null) $DatabaseConnection = new DatabaseConnection(); return $DatabaseConnection; } } ?> Usage: <?php $query = 'select * from mytable where foo = ? and bar = ?'; var_dump(DatabaseConnection::GetInstance()->getRecords($query, 'is', 1337, 'somestring'); $query = 'update mytable set foo = ? where bar = ?'; var_dump(DatabaseConnection::GetInstance()->executeQuery($query, 'is', 1337, 'somestring'); ?>

« previous php.notes (#165338) next »