note 95871 added to mysqli-stmt.bind-param
| From: | Anonymous at osu1 dot php dot net | Date: | Tue, 26 Jan 2010 22:24:10 +0000 |
| Subject: | note 95871 added to mysqli-stmt.bind-param | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165337@lists.php.net to get a copy of this message | ||
<?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');
?>
----
Server IP: 69.147.83.197
Probable Submitter: 80.57.33.206
----
Manual Page -- http://www.php.net/manual/en/mysqli-stmt.bind-param.php
Edit -- https://master.php.net/note/edit/95871
Del: integrated -- https://master.php.net/note/delete/95871/integrated
Del: useless -- https://master.php.net/note/delete/95871/useless
Del: bad code -- https://master.php.net/note/delete/95871/bad+code
Del: spam -- https://master.php.net/note/delete/95871/spam
Del: non-english -- https://master.php.net/note/delete/95871/non-english
Del: in docs -- https://master.php.net/note/delete/95871/in+docs
Del: other reasons-- https://master.php.net/note/delete/95871
Reject -- https://master.php.net/note/reject/95871
Search -- https://master.php.net/manage/user-notes.php