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
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.