<?
class Database extends Mysqli {
protected $host = 'localhost';
protected $port = '3306';
protected $socket = null;
protected $username = 'xxxxx';
protected $password = 'xxxxx';
protected $database = 'xxxxx';
public function __construct($host = null, $username = null, $password = null, $database = null, $port = null) {
if (isset($host)) {
$this->host = $host;
}
if (isset($username)) {
$this->username = $username;
}
if (isset($password)) {
$this->password = $password;
}
if (isset($database)) {
$this->database = $database;
}
if (isset($port)) {
$this->port = $port;
if ($port == 3306 && ($host == "127.0.0.1" || $host == "localhost")) {
$this->socket = "/tmp/mysql.sock";
$this->host = "localhost";
}
}
$this->link = parent::__construct($this->host, $this->username, $this->password, $this->database, $this->port, $this->socket);
if (mysqli_connect_errno()) {
throw new MySQLIConnectException("Unable to connect to $this->host");
}
}
public function __destruct() {
$this->close();
}
public function execute($sql) {
//-= TODO: access expanding full prepared statement methodology here...
if (!$this->ping()) {
throw new MysqliSQLException("Unable to connect, DAVE!");
}
if ($statement = $this->prepare($sql)) {
$statement->execute();
$statement->close();
} else {
throw new MysqliSQLException("$sql\n". mysqli_error($this));
}
}
public function query($sql) {
$result = parent::query($sql);
if (mysqli_error($this)) {
throw new MysqliSQLException("$sql\n". mysqli_error($this));
}
//-= Arggg, it's attaching to multi-query results here. getQuery shall remain singular!!!
do {
mysqli_next_result($this);
} while (mysqli_next_result($this));
return $result;
}
public function getQuery($sql) {
return $this->query($sql);
}
public function getObject($sql) {
$result = $this->query($sql);
$object = mysqli_fetch_object($result);
if (mysqli_error($this)) {
throw new MysqliSQLException("$sql\n". mysqli_error($this));
}
return $object;
}
public function getXMLFromSQL($sql, $roottag = 'results', $tagname = 'row', $meta = false) {
return $this->getXMLFromResult($this->getQuery($sql), $roottag, $tagname, $meta);
}
public function getXMLFromResult($result, $roottag = 'results', $tagname = 'row', $meta = false) {
if (!($result instanceof mysqli_result)) {
throw new MySQLIToXMLException("expects argument 1 to be of type `mysqli_result`");
}
$dom = new DomDocument("1.0", "UTF-8");
$root = $dom->createElement($roottag);
$dom->appendChild($root);
if ($meta) {
$fields = $result->fetch_fields();
$columns = $dom->createElement('meta');
foreach($fields as $field) {
$f = $dom->createElement('field');
$n = $dom->createAttribute('name');
$n->appendChild($dom->createTextNode($field->name));
$f->appendChild($n);
$n = $dom->createAttribute('table');
$n->appendChild($dom->createTextNode($field->table));
$f->appendChild($n);
$n = $dom->createAttribute('max_length');
$n->appendChild($dom->createTextNode($field->max_length));
$f->appendChild($n);
$n = $dom->createAttribute('type');
$n->appendChild($dom->createTextNode($field->type));
$f->appendChild($n);
$columns->appendChild($f);
}
$root->appendChild($columns);
}
while ($row = mysqli_fetch_object($result)) {
$r = $dom->createElement($tagname);
foreach($row as $key => $value) {
$c = $dom->createElement($key);
$c->appendChild($dom->createTextNode($value));
$r->appendChild($c);
}
$root->appendChild($r);
}
return $dom;
}
function escape($string) {
return mysqli_escape_string($this, $string);
}
function nullIfEmpty($str) {
$str = trim($str);
if ($str == "0") {
return "NULL";
}
if (strlen($str) > 0) {
return "'". $this->escape($str) ."'";
}
return "NULL";
}
}
class MySQLIConnectException extends Exception {}
class MySQLISQLException extends Exception {}
class MySQLIToXMLException extends Exception {}
if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
require_once "ErrorHandler.php";
$db = new Database();
$result = $db->getObject("SELECT count(*) thecount FROM database.table");
echo $result->thecount;
#$dom = $db->getXMLFromSQL("call getCustomer(12345);", 'customers', 'customer');
#header("Content-type: text/xml");
#echo $dom->saveXML();
}
?>