note 102741 deleted from mysqli-stmt.bind-param by danbrown
| From: | danbrown@php.net | Date: | Thu, 03 Mar 2011 13:58:03 +0000 |
| Subject: | note 102741 deleted from mysqli-stmt.bind-param by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177527@lists.php.net to get a copy of this message | ||
Note Submitter: stormbyte at gmail dot com
----
I borrowed the code from canche_x at yahoo dot com and modified a bit to create an abstract class to
be inherited, to handle easily the use of prepared statements, and even to auto alias it to a string
name.
I hope someone gets this usefull as I did many times :)
Example of usage:
<?php
require_once 'MySQLDatabase.php';
class MyDatabase extends MySQLDatabase {
private function PrepareAllStatements() {
$this->PrepareSTMT("test1", "SELECT COUNT(id) AS countid FROM users");
$this->PrepareSTMT("test2", "UPDATE users SET pass_hash=? WHERE id=?");
// ...
}
public function __construct() {
parent::__construct();
$this->Connect("localhost", "foo", "bar", "mywebdb");
$this->PrepareAllStatements();
}
public function __destruct() {
parent::__destruct();
}
public function GetTotalUsers() {
$result=0;
$tmp=$this->ExecuteQuery("test1");
if (isset($tmp[0]['countid']) {
$result=$tmp[0]['countid'];
}
return $result;
}
public function UpdatePass($userid, $hash) {
$affected_rows=$this->ExecuteQuery("test2", $userid, $hash);
return ($affected_rows==1);
}
//...
};
//To use it is simple! Example of usage:
$db=new MyDatabase();
echo "I have ".$db->GetTotalUsers()." users now.<br />";
if ($db->UpdatePass(1, "somehash")) {
echo "Password succesfully updated for user #1";
}
else {
echo "Update failed!<br />";
}
?>
The code itself is a bit large, but it is published in my blog: http://www.stormbyte.blogspot.com as well as in
pastebin: http://pastebin.com/ccZqgT5M