<?php
/*
Date: 2006/05/25
Author: Braian Gómez. <hide@address.com>
Name: MySQLo
Version: 1.0
Language: PHP 4
Description: MySQLo, MySQL Object is a class to make connections to MySQL databases.
It provides more or less the same syntax as ADOdb to make it compatible. Some functions are not supported.
This is like a striped down version of ADOdb just for MySQL connections. Recordset aren't supported
Functions available are:
Execute() (it returns a RESOURCE instead of a RECORDSET)
GetArray()
GetCol()
GetRow()
GetOne()
InsertID()
ErrorMsg()
SelectDB()
Connect()
PConnect()
Close()
VairuX Systems - http://www.vairux.com
*/
class MySQLo {
var $_dbQuery = null;
var $_dbConnection = false;
var $_dbDSN = null;
var $_dbUname = null;
var $_dbPass = null;
var $_dbDatabase = null;
var $_dbPort = null;
var $_dbHost = null;
var $_errorMsg = null;
// Example: mysql://user:hide@address.com:port/database
function _setDSN($dsn) {
if ($at = strpos($dsn,'://')) {
$this->_dbDSN = $dsn;
$dsna = @parse_url($dsn);
$dsn = @$dsna['scheme'];
if (!$dsn) return FALSE;
$this->_dbHost = isset($dsna['host']) ? rawurldecode($dsna['host']) : '';
$this->_dbPort = isset($dsna['port']) ? rawurldecode($dsna['port']) : '';
$this->_dbUname = isset($dsna['user']) ? rawurldecode($dsna['user']) : '';
$this->_dbPass = isset($dsna['pass']) ? rawurldecode($dsna['pass']) : '';
$this->_dbDatabase = isset($dsna['path']) ? rawurldecode(substr($dsna['path'],1)) : ''; # strip off initial /
}
}
// This function builds a query based on the input array if exists.
// Doesn't supports arrays in $sql variable.
function _BuildQueryArray($sql, $inputarr){
if ($inputarr) {
if (!is_array($inputarr)) $inputarr = array($inputarr);
$element0 = reset($inputarr);
# is_object check because oci8 descriptors can be passed in
$array_2d = is_array($element0) && !is_object(reset($element0));
//remove extra memory copy of input -mikefedyk
unset($element0);
if (!is_array($sql)) {
$sqlarr = explode('?',$sql);
if (!$array_2d) $inputarr = array($inputarr);
foreach($inputarr as $arr) {
$sql = ''; $i = 0;
//Use each() instead of foreach to reduce memory usage -mikefedyk
while(list(, $v) = each($arr)) {
$sql .= $sqlarr[$i];
// from Ron Baldwin <ron.baldwin#sourceprose.com>
// Only quote string types
$typ = gettype($v);
if ($typ == 'string')
//New memory copy of input created here -mikefedyk
$sql .= $this->qstr($v);
else if ($typ == 'double')
$sql .= str_replace(',','.',$v); // locales fix so 1.1 does not get converted to 1,1
else if ($typ == 'boolean')
$sql .= $v ? TRUE : FALSE;
else if ($v === null)
$sql .= 'NULL';
else
$sql .= $v;
$i += 1;
}
if (isset($sqlarr[$i])) {
$sql .= $sqlarr[$i];
if ($i+1 != sizeof($sqlarr)) $this->outp("Input Array does not match ?: ".htmlspecialchars($sql));
} else if ($i != sizeof($sqlarr))
$this->outp("Input array does not match ?: ".htmlspecialchars($sql));
}
}
}
return $sql;
}
// Prints messages
function outp($message){
echo $message;
}
// This function is capable to execute DDL's and INSERT, UPDATE statements. SELECT statements could return true or false.
function &Execute($sql, $inputarr = null) {
if ($inputarr != null){
$sql = $this->_BuildQueryArray($sql, $inputarray);
}
$this->_query($sql);
echo $this->ErrorMsg();
return $this->_dbQuery;
}
// Execute MySQL query
function &_query($sql) {
$this->_dbQuery = mysql_query($sql, $this->_dbConnection);
$this->_errorMsg = mysql_error();
return $this->_dbQuery;
}
// Returns the query as an array.
function &GetArray ($sql, $params = null) {
$this->Execute($sql, $params);
$result = array();
while ($row = mysql_fetch_array($this->_dbQuery)) {
array_push($result, $row);
}
mysql_free_result($this->_dbQuery);
return $result;
}
// Returns the first row as an associative array.
function &GetRow ($sql, $params = null) {
$this->Execute($sql, $params);
$result = mysql_fetch_array($this->_dbQuery);
mysql_free_result($this->_dbQuery);
return $result;
}
// Returns the first column values as an array,
function &GetCol($sql, $params = null) {
$result = array();
$query = $this->GetArray($sql, $params);
while(list(,$val) = each($query)) {
array_push($result, $val[0]);
}
return $result;
}
// Returns the first value of the first column in the query.
function GetOne ($sql, $params = null) {
$this->_query($sql, $params);
return @mysql_result($this->_dbQuery, 0,0);
}
function InsertID(){
return mysql_insert_id($this->_dbConnection);
}
function ErrorMsg()
{
if ($this->_errorMsg) return '!! '.$this->_errorMsg;
else return '';
}
// Selects Database to work
function SelectDB($dbName){
$this->_dbDatabase = $dbName;
if ($this->_dbConnection) {
return @mysql_select_db($dbName,$this->_dbConnection);
}
else return false;
}
// Connect to database
function Connect($argHostname, $argUsername=null, $argPassword=null, $argDatabasename=null){
if ($this->_dbConnection == false) {
if ($argUsername == null && $argPassword == null && $argDatabasename == null) {
$this->_setDSN($argHostname);
} else {
$this->_dbHost = $argHostname;
$this->_dbUname = $argUsername;
$this->_dbPass = $argPassword;
$this->_dbDatabase = $argDatabasename;
}
$argHostname = $this->_dbHost;
if (!empty($this->_dbPort)) $argHostname .= ":".$this->port;
$this->_dbConnection = mysql_connect($argHostname, $this->_dbUname, $this->_dbPass);
if ($this->_dbConnection === false) return false;
if ($this->_dbDatabase) return $this->SelectDB($this->_dbDatabase);
return true;
}
}
// Connect to database (persistent connection)
function PConnect($argHostname, $argUsername=null, $argPassword=null, $argDatabasename=null){
if ($this->_dbConnection == false) {
if ($argUsername == null && $argPassword == null && $argDatabasename == null) {
$this->_setDSN($argHostname);
} else {
$this->_dbHost = $argHostname;
$this->_dbUname = $argUsername;
$this->_dbPass = $argPassword;
$this->_dbDatabase = $argDatabasename;
}
$argHostname = $this->_dbHost;
if (!empty($this->_dbPort)) $argHostname .= ":".$this->port;
$this->_dbConnection = mysql_pconnect($argHostname, $this->_dbUname, $this->_dbPass);
if ($this->_dbConnection === false) return false;
if ($this->_dbDatabase) return $this->SelectDB($this->_dbDatabase);
return true;
}
}
// Close connection to database
function Close(){
@mysql_close($this->_dbConnection);
$this->_dbConnection = false;
}
// Constructors and Destructors.
// Default constructor.
function MySQLo() {
$this->_dbPort = "3360";
$this->_dbHost = "localhost";
register_shutdown_function( array( &$this, "__destroy" ) );
}
// Constructor with dsn direct connection.
function MySQLo($dsn) {
$this->Connect($dsn);
register_shutdown_function(array( &$this, "__destroy" ));
}
// Destructor. Close database on destroy.
function __destroy() {
$this->Close();
}
}
?>