Name: MySQLo Version: 1.0.3 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 (http://adodb.sf.net) 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 Changelog: 1.0.1: Fixed register_shutdown_function bug on passing references. 1.0.2: Added basic support for transactions, using ADOdb style. 1.0.3: Fixed constructor 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:pass@host: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 // 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; } // if magic quotes disabled, use mysql_real_escape_string() function qstr($s,$magic_quotes=false) { if (!$magic_quotes) { //if (ADODB_PHPVER >= 0x4300) { if (is_resource($this->_connectionID)) return "'".mysql_real_escape_string($s,$this->_connectionID)."'"; //} /* if ($this->replaceQuote[0] == '\\'){ $s = adodb_str_replace(array('\\',"\0"),array('\\\\',"\\\0"),$s); } */ return "'".str_replace("'",$this->replaceQuote,$s)."'"; } // undo magic quotes for " $s = str_replace('\\"','"',$s); return "'$s'"; } // 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. It returns a resource instead of a RecordSet like ADOdb function &Execute($sql, $inputarray = null) { if ($inputarray != 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; } // v1.0.1 Register destructor once stablished $this->_dbConnection. Otherwise it will not close. register_shutdown_function(array( &$this, "__destroy" )); } // 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; } // v1.0.1 Register destructor once stablished $this->_dbConnection. Otherwise it will not close. register_shutdown_function(array( &$this, "__destroy" )); } // Close connection to database function Close(){ if ($this->_dbConnection !== false) { // 1.0.1 Added @mysql_close($this->_dbConnection); $this->_dbConnection = false; } } /** Basic support for transactions (InnoDB, BDB) Since v1.0.2 **/ function BeginTrans() { return $this->Execute("BEGIN"); } function StartTrans() { return $this->BeginTrans(); } function CommitTrans($ok = true) { if ($ok) { return $this->Execute("COMMIT"); } else { return $this->RollbackTrans(); } } function RollbackTrans() { return $this->Execute("ROLLBACK"); } function CompleteTrans() { if ($this->_errorMsg != null) { return $this->RollbackTrans(); } else { return $this->CommitTrans(); } } /** **/ // SelectLimit function returns an Array instead of returning a RecordSet like ADOdb. To be implemented. (Since v 1.0.2) function &SelectLimit($sql,$numrows=-1,$offset=-1,$inputarr=false) { return $this->SelectLimitArray($sql, $numrows, $offset, $inputarr); } // This is the SelectLimit that returns an Array instead of a RecordSet. (Since v 1.0.2) function &SelectLimitArray($sql,$numrows=-1,$offset=-1,$inputarr=false) { // Taken from ADOdb's MySQL driver. $offsetStr =($offset>=0) ? ((integer)$offset)."," : ''; // jason judge, see http://phplens.com/lens/lensforum/msgs.php?id=9220 if ($nrows < 0) $nrows = '18446744073709551615'; return $this->GetArray($sql." LIMIT ".$offsetStr.((integer)$nrows),$inputarr); } // Constructors and Destructors. // Default constructor. function MySQLo($dsn = null) { if ($dsn != null) { $this->Connect($dsn); } else { $this->_dbPort = "3360"; $this->_dbHost = "localhost"; } } // Destructor. Close database on destroy. function __destroy() { $this->Close(); } } ?>