Source for file adodb-mysql.inc.php
Documentation is available at adodb-mysql.inc.php
V4.97 22 Jan 2008 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved.
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence.
MySQL code that does not support transactions. Use mysqlt if you need transactions.
Requires mysql client. Works on Windows and Unix.
28 Feb 2001: MetaColumns bug fix - suggested by Freek Dijkstra (phpeverywhere@macfreek.com)
if (! defined("_ADODB_MYSQL_LAYER")) {
define("_ADODB_MYSQL_LAYER", 1 );
var $hasGenID = false; // Fix for PostNuke Pablo Roca / larsneo / Neo
var $isoDates = true; // accepts dates in ISO format
var $nameQuote = '`'; /// string to use to quote identifiers and names
var $compat323 = false; // true if compat with mysql 3.23
function IfNull( $field, $ifNull )
return " IFNULL($field, $ifNull) "; // if MySQL
function &MetaTables($ttype= false,$showSchema= false,$mask= false)
$mask = $this->qstr($mask);
function &MetaIndexes ($table, $primary = FALSE, $owner= false)
global $ADODB_FETCH_MODE;
$save = $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = $save;
// parse index data into array
while ($row = $rs->FetchRow()) {
if ($primary == FALSE AND $row[2] == 'PRIMARY') {
if (!isset ($indexes[$row[2]])) {
$indexes[$row[2]] = array(
'unique' => ($row[1] == 0),
$indexes[$row[2]]['columns'][$row[3] - 1] = $row[4];
// sort columns by order in the index
ksort ($indexes[$index]['columns']);
// if magic quotes disabled, use mysql_real_escape_string()
function qstr($s,$magic_quotes= false)
return "'". mysql_real_escape_string($s,$this->_connectionID). "'";
$s = adodb_str_replace(array('\\',"\0"),array('\\\\',"\\\0"),$s);
// undo magic quotes for "
//return mysql_insert_id($this->_connectionID);
function GetOne($sql,$inputarr= false)
if ($rs->EOF) return false;
return reset($rs->fields);
if ($this->debug) ADOConnection::outp("Transactions not supported in 'mysql' driver. Use 'mysqlt' or 'mysqli' driver");
// See http://www.mysql.com/doc/M/i/Miscellaneous_functions.html
// Reference on Last_Insert_ID on the recommended way to simulate sequences
var $_genIDSQL = "update %s set id=LAST_INSERT_ID(id+1);";
return $this->Execute(sprintf($this->_genSeqzikula. orgame,$startID- 1));
function GenID($seqname= 'adodbseq',$startID= 1)
// post-nuke sets hasGenID to false
// if (!$this->hasGenID) return false;
$holdtransOK = $this->_transOK; // save the current status
if ($holdtransOK) $this->_transOK = true; //if the status was ok before reset
if (!$cnt) $this->Execute(sprintf($this->_genSeqzikula. orgame,$startID- 1));
$max = mysql_num_rows($qid);
$db = mysql_tablename($qid,$i);
if ($db != 'mysql') $arr[] = $db;
// Format date column in sql string given an input format that understands Y M D
$s = 'DATE_FORMAT('. $col. ",'";
for ($i= 0; $i < $len; $i++ ) {
$s .= "'),Quarter($col)";
if ($len > $i+ 1) $s .= ",DATE_FORMAT($col,'";
if ($concat) $s = "CONCAT($s)";
// returns concatenated string
// much easier to run "mysqld --ansi" or "mysqld --sql-mode=PIPES_AS_CONCAT" and use || operator
// suggestion by andrew005@mnogo.ru
if (strlen($s) > 0) return "CONCAT($s)";
if (!$date) $date = $this->sysDate;
$fraction = $dayFraction * 24 * 3600;
return $date . ' + INTERVAL ' . $fraction. ' SECOND';
// return "from_unixtime(unix_timestamp($date)+$fraction)";
function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
if (!empty($this->port)) $argHostname .= ":". $this->port;
$this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword,
$this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword,
$this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword);
if ($argDatabasename) return $this->SelectDB($argDatabasename);
function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
if (!empty($this->port)) $argHostname .= ":". $this->port;
$this->_connectionID = mysql_pconnect($argHostname,$argUsername,$argPassword);
if ($argDatabasename) return $this->SelectDB($argDatabasename);
function _nconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename);
global $ADODB_FETCH_MODE;
$save = $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = $save;
$fld->name = $rs->fields[0];
// split type into type(length):
if (preg_match("/^(.+)\((\d+),(\d+)/", $type, $query_array)) {
$fld->type = $query_array[1];
$fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : - 1;
$fld->scale = is_numeric($query_array[3]) ? $query_array[3] : - 1;
} elseif (preg_match("/^(.+)\((\d+)/", $type, $query_array)) {
$fld->type = $query_array[1];
$fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : - 1;
} elseif (preg_match("/^(enum)\((.*)\)$/i", $type, $query_array)) {
$fld->type = $query_array[1];
$arr = explode(",",$query_array[2]);
$fld->max_length = ($zlen > 0) ? $zlen : 1;
$fld->not_null = ($rs->fields[2] != 'YES');
$fld->primary_key = ($rs->fields[3] == 'PRI');
$fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false);
if ($d != '' && $d != 'NULL') {
$fld->has_default = true;
$fld->default_value = $d;
$fld->has_default = false;
$this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions
// parameters use PostgreSQL convention, not MySQL
function &SelectLimit($sql,$nrows=- 1,$offset=- 1,$inputarr= false,$secs= 0)
$offsetStr = ($offset>= 0) ? ((integer) $offset). "," : '';
// jason judge, see http://phplens.com/lens/lensforum/msgs.php?id=9220
if ($nrows < 0) $nrows = '18446744073709551615';
$rs = & $this->CacheExecute($secs,$sql. " LIMIT $offsetStr". ((integer) $nrows),$inputarr);
$rs = & $this->Execute($sql. " LIMIT $offsetStr". ((integer) $nrows),$inputarr);
// returns queryID or false
function _query($sql,$inputarr)
//global $ADODB_COUNTRECS;
//else return @mysql_unbuffered_query($sql,$this->_connectionID); // requires PHP >= 4.0.6
/* Returns: the last error message from previous database operation */
/* Returns: the last error number from previous database operation */
* Maximum size of C field
* Maximum size of X field
// "Innox - Juan Carlos Gonzalez" <jgonzikula.orgnox.com.mx>
function MetaForeignKeys( $table, $owner = FALSE, $upper = FALSE, $associative = FALSE )
global $ADODB_FETCH_MODE;
$table = "$owner.$table";
$a_create_table = $this->getRow(sprintf('SHOW CREATE TABLE %s', $table));
if ($associative) $create_sql = $a_create_table["Create Table"];
else $create_sql = $a_create_table[1];
if (!preg_match_all("/FOREIGN KEY \(`(.*?)`\) REFERENCES `(.*?)` \(`(.*?)`\)/", $create_sql, $matches)) return false;
$num_keys = count($matches[0]);
for ( $i = 0; $i < $num_keys; $i ++ ) {
$my_field = explode('`, `', $matches[1][$i]);
$ref_table = $matches[2][$i];
$ref_field = explode('`, `', $matches[3][$i]);
$foreign_keys[$ref_table] = array();
$num_fields = count($my_field);
for ( $j = 0; $j < $num_fields; $j ++ ) {
$foreign_keys[$ref_table][$ref_field[$j]] = $my_field[$j];
$foreign_keys[$ref_table][] = "{ $my_field[$j]}={ $ref_field[$j]}";
/*--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------*/
global $ADODB_FETCH_MODE;
$mode = $ADODB_FETCH_MODE;
$this->adodbFetchMode = $mode;
//GLOBAL $ADODB_COUNTRECS;
// $this->_numOfRows = ($ADODB_COUNTRECS) ? @mysql_num_rows($this->_queryID):-1;
if ($fieldOffset != - 1) {
$o = @mysql_fetch_field($this->_queryID, $fieldOffset);
$f = @mysql_field_flags($this->_queryID,$fieldOffset);
$o->max_length = @mysql_field_len($this->_queryID,$fieldOffset); // suggested by: Jim Nicholson (jnich#att.com)
//$o->max_length = -1; // mysql returns the max length less spaces -- so it is unrealiable
$o->binary = (strpos($f,'binary')!== false);
else if ($fieldOffset == - 1) { /* The $fieldOffset argument is not provided thus its -1 */
$o = @mysql_fetch_field($this->_queryID);
$o->max_length = @mysql_field_len($this->_queryID); // suggested by: Jim Nicholson (jnich#att.com)
//$o->max_length = -1; // mysql returns the max length less spaces -- so it is unrealiable
/* Use associative array to get fields array */
// added @ by "Michael William Miller" <mille562@pilot.msu.edu>
return @mysql_data_seek($this->_queryID,$row);
//return adodb_movenext($this);
//if (defined('ADODB_EXTENSION')) return adodb_movenext($this);
function MetaType($t,$len=- 1,$fieldobj= false)
$len = $fieldobj->max_length;
$len = - 1; // mysql max_length is not accurate
if ($len <= $this->blobSize) return 'C';
// php_mysql extension always returns 'blob' even if 'text'
// so we have to check whether binary...
return !empty($fieldobj->binary) ? 'B' : 'X';
case 'TIMESTAMP': return 'T';
if (!empty($fieldobj->primary_key)) return 'R';
global $ADODB_FETCH_MODE;
$mode = $ADODB_FETCH_MODE;
$this->adodbFetchMode = $mode;
return @adodb_movenext($this);
|