dotProject 数据库中文乱码解决方案-1

本文介绍如何在MySQL中正确配置UTF8编码,确保数据库能够正确处理和存储Unicode字符。包括数据库连接参数调整、PHP代码示例及数据库默认字符集设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先感谢博主Mark Wu(http://blog.markplace.net/marks_development_blog/19/2007/04/29/258),本文参照他提供的解决方法进行处理的。

1. 先用 phpMyAdmin 建立一個 database 連線校對(collation)使用 utf8_general_ci 或 utf8_unicode_ci

2. 修改 dotproject/lib/adodb/drivers/adodb-mysql.inc.php
function _connect 及 function _pconnect

if ($this->_connectionID === false) return false;
下面加上


if ($this->_isSupportUtf8() && $argDatabasename) {
            $dbEncoding = $this->_getDbDefaultEncoding($argDatabasename);
            if ($dbEncoding) {
                mysql_query("SET NAMES $dbEncoding", $this->_connectionID);
            }
        }
       
           // trun off strict mode
        mysql_query("SET sql_mode = '';");





增加如下函数:


function _isSupportUtf8() {
        // check mysql version first. Version lower than 4.1 doesn't support utf8
        $serverVersion = mysql_get_server_info($this->_connectionID);
        $version = explode('.', $serverVersion);
        if ($version[0] < 4) return false;
        if ( ($version[0] == 4) && ($version[1] == 0) ) return false;
       
        // check if utf8 support was compiled in
        $result = mysql_query("SHOW CHARACTER SET like 'utf8'", $this->_connectionID);
        if (mysql_num_rows($result) > 0) {
            return true;
        }
        return false;
    }
   
    function _getDbDefaultEncoding($argDatabasename){
        if (!$argDatabasename) {
            return false;
        }
       
        // We use a SHOW CREATE DATABASE command to show the original
        // SQL character set when DB was created.
        $result = mysql_query("SHOW CREATE DATABASE `$argDatabasename`", $this->_connectionID);
        if (mysql_num_rows($result) < 0 ) {
            // The specified db name is wrong!
            return false;
        }
        $dbInfo = mysql_fetch_row($result);
        $pattern = '/40100 DEFAULT CHARACTER SET (/w+) /';
        if ( (preg_match($pattern, $dbInfo[1], $match) > 0) ) {
            return $match[1];
        }
        return false;
    }




通过以上修改就可以正常的从 phpMyAdmin 或 mysql 中來察看和修改中文了。



贴出完整的dotproject/lib/adodb/drivers/adodb-mysql.inc.php代码以供参考,本版本是2.1.2;


  1. <?php
  2. /*
  3. V4.72 21 Feb 2006  (c) 2000-2006 John Lim (jlim@natsoft.com.my). All rights reserved.
  4.   Released under both BSD license and Lesser GPL library license. 
  5.   Whenever there is any discrepancy between the two licenses, 
  6.   the BSD license will take precedence.
  7.   Set tabs to 8.
  8.   
  9.   MySQL code that does not support transactions. Use mysqlt if you need transactions.
  10.   Requires mysql client. Works on Windows and Unix.
  11.   
  12.  28 Feb 2001: MetaColumns bug fix - suggested by  Freek Dijkstra (phpeverywhere@macfreek.com)
  13. */ 
  14. // security - hide paths
  15. if (!defined('ADODB_DIR')) die();
  16. if (! defined("_ADODB_MYSQL_LAYER")) {
  17.  define("_ADODB_MYSQL_LAYER", 1 );
  18. class ADODB_mysql extends ADOConnection {
  19.     var $databaseType = 'mysql';
  20.     var $dataProvider = 'mysql';
  21.     var $hasInsertID = true;
  22.     var $hasAffectedRows = true;    
  23.     var $metaTablesSQL = "SHOW TABLES"
  24.     var $metaColumnsSQL = "SHOW COLUMNS FROM %s";
  25.     var $fmtTimeStamp = "'Y-m-d H:i:s'";
  26.     var $hasLimit = true;
  27.     var $hasMoveFirst = true;
  28.     var $hasGenID = true;
  29.     var $isoDates = true; // accepts dates in ISO format
  30.     var $sysDate = 'CURDATE()';
  31.     var $sysTimeStamp = 'NOW()';
  32.     var $hasTransactions = false;
  33.     var $forceNewConnect = false;
  34.     var $poorAffectedRows = true;
  35.     var $clientFlags = 0;
  36.     var $substr = "substring";
  37.     var $nameQuote = '`';       /// string to use to quote identifiers and names
  38.     
  39.     function ADODB_mysql() 
  40.     {           
  41.         if (defined('ADODB_EXTENSION')) $this->rsPrefix .= 'ext_';
  42.     }
  43.     
  44.     function ServerInfo()
  45.     {
  46.         $arr['description'] = ADOConnection::GetOne("select version()");
  47.         $arr['version'] = ADOConnection::_findvers($arr['description']);
  48.         return $arr;
  49.     }
  50.     
  51.     function IfNull( $field$ifNull ) 
  52.     {
  53.         return " IFNULL($field, $ifNull) "// if MySQL
  54.     }
  55.     
  56.     
  57.     function &MetaTables($ttype=false,$showSchema=false,$mask=false) 
  58.     {   
  59.         $save = $this->metaTablesSQL;
  60.         if ($showSchema && is_string($showSchema)) {
  61.             $this->metaTablesSQL .= " from $showSchema";
  62.         }
  63.         
  64.         if ($mask) {
  65.             $mask = $this->qstr($mask);
  66.             $this->metaTablesSQL .= " like $mask";
  67.         }
  68.         $ret =& ADOConnection::MetaTables($ttype,$showSchema);
  69.         
  70.         $this->metaTablesSQL = $save;
  71.         return $ret;
  72.     }
  73.     
  74.     
  75.     function &MetaIndexes ($table$primary = FALSE, $owner=false)
  76.     {
  77.         // save old fetch mode
  78.         global $ADODB_FETCH_MODE;
  79.         
  80.         $false = false;
  81.         $save = $ADODB_FETCH_MODE;
  82.         $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  83.         if ($this->fetchMode !== FALSE) {
  84.                $savem = $this->SetFetchMode(FALSE);
  85.         }
  86.         
  87.         // get index details
  88.         $rs = $this->Execute(sprintf('SHOW INDEX FROM %s',$table));
  89.         
  90.         // restore fetchmode
  91.         if (isset($savem)) {
  92.                 $this->SetFetchMode($savem);
  93.         }
  94.         $ADODB_FETCH_MODE = $save;
  95.         
  96.         if (!is_object($rs)) {
  97.                 return $false;
  98.         }
  99.         
  100.         $indexes = array ();
  101.         
  102.         // parse index data into array
  103.         while ($row = $rs->FetchRow()) {
  104.                 if ($primary == FALSE AND $row[2] == 'PRIMARY') {
  105.                         continue;
  106.                 }
  107.                 
  108.                 if (!isset($indexes[$row[2]])) {
  109.                         $indexes[$row[2]] = array(
  110.                                 'unique' => ($row[1] == 0),
  111.                                 'columns' => array()
  112.                         );
  113.                 }
  114.                 
  115.                 $indexes[$row[2]]['columns'][$row[3] - 1] = $row[4];
  116.         }
  117.         
  118.         // sort columns by order in the index
  119.         foreach ( array_keys ($indexesas $index )
  120.         {
  121.                 ksort ($indexes[$index]['columns']);
  122.         }
  123.         
  124.         return $indexes;
  125.     }
  126.     
  127.     // if magic quotes disabled, use mysql_real_escape_string()
  128.     function qstr($s,$magic_quotes=false)
  129.     {
  130.         if (!$magic_quotes) {
  131.         
  132.             if (ADODB_PHPVER >= 0x4300) {
  133.                 if (is_resource($this->_connectionID))
  134.                     return "'".mysql_real_escape_string($s,$this->_connectionID)."'";
  135.             }
  136.             if ($this->replaceQuote[0] == '//'){
  137.                 $s = adodb_str_replace(array('//',"/0"),array('////',"///0"),$s);
  138.             }
  139.             return  "'".str_replace("'",$this->replaceQuote,$s)."'"
  140.         }
  141.         
  142.         // undo magic quotes for "
  143.         $s = str_replace('//"','"',$s);
  144.         return "'$s'";
  145.     }
  146.     
  147.     function _insertid()
  148.     {
  149.         return ADOConnection::GetOne('SELECT LAST_INSERT_ID()');
  150.         //return mysql_insert_id($this->_connectionID);
  151.     }
  152.     
  153.     function GetOne($sql,$inputarr=false)
  154.     {
  155.         if (strncasecmp($sql,'sele',4) == 0) {
  156.             $rs =& $this->SelectLimit($sql,1,-1,$inputarr);
  157.             if ($rs) {
  158.                 $rs->Close();
  159.                 if ($rs->EOF) return false;
  160.                 return reset($rs->fields);
  161.             }
  162.         } else {
  163.             return ADOConnection::GetOne($sql,$inputarr);
  164.         }
  165.         return false;
  166.     }
  167.     
  168.     function BeginTrans()
  169.     {
  170.         if ($this->debug) ADOConnection::outp("Transactions not supported in 'mysql' driver. Use 'mysqlt' or 'mysqli' driver");
  171.     }
  172.     
  173.     function _affectedrows()
  174.     {
  175.             return mysql_affected_rows($this->_connectionID);
  176.     }
  177.   
  178.     // See http://www.mysql.com/doc/M/i/Miscellaneous_functions.html
  179.     // Reference on Last_Insert_ID on the recommended way to simulate sequences
  180.     var $_genIDSQL = "update %s set id=LAST_INSERT_ID(id+1);";
  181.     var $_genSeqSQL = "create table %s (id int not null)";
  182.     var $_genSeq2SQL = "insert into %s values (%s)";
  183.     var $_dropSeqSQL = "drop table %s";
  184.     
  185.     function CreateSequence($seqname='adodbseq',$startID=1)
  186.     {
  187.         if (emptyempty($this->_genSeqSQL)) return false;
  188.         $u = strtoupper($seqname);
  189.         
  190.         $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
  191.         if (!$okreturn false;
  192.         return $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1));
  193.     }
  194.     
  195.     function GenID($seqname='adodbseq',$startID=1)
  196.     {
  197.         // post-nuke sets hasGenID to false
  198.         if (!$this->hasGenID) return false;
  199.         
  200.         $savelog = $this->_logsql;
  201.         $this->_logsql = false;
  202.         $getnext = sprintf($this->_genIDSQL,$seqname);
  203.         $holdtransOK = $this->_transOK; // save the current status
  204.         $rs = @$this->Execute($getnext);
  205.         if (!$rs) {
  206.             if ($holdtransOK$this->_transOK = true; //if the status was ok before reset
  207.             $u = strtoupper($seqname);
  208.             $this->Execute(sprintf($this->_genSeqSQL,$seqname));
  209.             $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1));
  210.             $rs = $this->Execute($getnext);
  211.         }
  212.         $this->genID = mysql_insert_id($this->_connectionID);
  213.         
  214.         if ($rs$rs->Close();
  215.         
  216.         $this->_logsql = $savelog;
  217.         return $this->genID;
  218.     }
  219.     
  220.     function &MetaDatabases()
  221.     {
  222.         $qid = mysql_list_dbs($this->_connectionID);
  223.         $arr = array();
  224.         $i = 0;
  225.         $max = mysql_num_rows($qid);
  226.         while ($i < $max) {
  227.             $db = mysql_tablename($qid,$i);
  228.             if ($db != 'mysql'$arr[] = $db;
  229.             $i += 1;
  230.         }
  231.         return $arr;
  232.     }
  233.     
  234.         
  235.     // Format date column in sql string given an input format that understands Y M D
  236.     function SQLDate($fmt$col=false)
  237.     {   
  238.         if (!$col$col = $this->sysTimeStamp;
  239.         $s = 'DATE_FORMAT('.$col.",'";
  240.         $concat = false;
  241.         $len = strlen($fmt);
  242.         for ($i=0; $i < $len$i++) {
  243.             $ch = $fmt[$i];
  244.             switch($ch) {
  245.                 
  246.             default:
  247.                 if ($ch == '//') {
  248.                     $i++;
  249.                     $ch = substr($fmt,$i,1);
  250.                 }
  251.                 /** FALL THROUGH */
  252.             case '-':
  253.             case '/':
  254.                 $s .= $ch;
  255.                 break;
  256.                 
  257.             case 'Y':
  258.             case 'y':
  259.                 $s .= '%Y';
  260.                 break;
  261.             case 'M':
  262.                 $s .= '%b';
  263.                 break;
  264.                 
  265.             case 'm':
  266.                 $s .= '%m';
  267.                 break;
  268.             case 'D':
  269.             case 'd':
  270.                 $s .= '%d';
  271.                 break;
  272.             
  273.             case 'Q':
  274.             case 'q':
  275.                 $s .= "'),Quarter($col)";
  276.                 
  277.                 if ($len > $i+1) $s .= ",DATE_FORMAT($col,'";
  278.                 else $s .= ",('";
  279.                 $concat = true;
  280.                 break;
  281.             
  282.             case 'H'
  283.                 $s .= '%H';
  284.                 break;
  285.                 
  286.             case 'h':
  287.                 $s .= '%I';
  288.                 break;
  289.                 
  290.             case 'i':
  291.                 $s .= '%i';
  292.                 break;
  293.                 
  294.             case 's':
  295.                 $s .= '%s';
  296.                 break;
  297.                 
  298.             case 'a':
  299.             case 'A':
  300.                 $s .= '%p';
  301.                 break;
  302.                 
  303.             case 'w':
  304.                 $s .= '%w';
  305.                 break;
  306.                 
  307.              case 'W':
  308.                 $s .= '%U';
  309.                 break;
  310.                 
  311.             case 'l':
  312.                 $s .= '%W';
  313.                 break;
  314.             }
  315.         }
  316.         $s.="')";
  317.         if ($concat$s = "CONCAT($s)";
  318.         return $s;
  319.     }
  320.     
  321.     // returns concatenated string
  322.     // much easier to run "mysqld --ansi" or "mysqld --sql-mode=PIPES_AS_CONCAT" and use || operator
  323.     function Concat()
  324.     {
  325.         $s = "";
  326.         $arr = func_get_args();
  327.         
  328.         // suggestion by andrew005@mnogo.ru
  329.         $s = implode(',',$arr); 
  330.         if (strlen($s) > 0) return "CONCAT($s)";
  331.         else return '';
  332.     }
  333.     
  334.     function OffsetDate($dayFraction,$date=false)
  335.     {       
  336.         if (!$date$date = $this->sysDate;
  337.         $fraction = $dayFraction * 24 * 3600;
  338.         return "from_unixtime(unix_timestamp($date)+$fraction)";
  339.     }
  340.     
  341.     // returns true or false
  342.     function _connect($argHostname$argUsername$argPassword$argDatabasename)
  343.     {
  344.         if (!emptyempty($this->port)) $argHostname .= ":".$this->port;
  345.         
  346.         if (ADODB_PHPVER >= 0x4300)
  347.             $this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword,
  348.                                                 $this->forceNewConnect,$this->clientFlags);
  349.         else if (ADODB_PHPVER >= 0x4200)
  350.             $this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword,
  351.                                                 $this->forceNewConnect);
  352.         else
  353.             $this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword);
  354.     
  355.         if ($this->_connectionID === false) return false;
  356.        

  357.         //new row -begin
  358.         if ($this->_isSupportUtf8() && $argDatabasename) {
  359.             $dbEncoding = $this->_getDbDefaultEncoding($argDatabasename);
  360.             if ($dbEncoding) {
  361.                 mysql_query("SET NAMES $dbEncoding"$this->_connectionID);
  362.             }
  363.         }
  364.         
  365.            // trun off strict mode
  366.         mysql_query("SET sql_mode = '';");
  367.        

  368.         //new row -end

  369.         if ($argDatabasenamereturn $this->SelectDB($argDatabasename);
  370.         return true;    
  371.     }
  372.     
  373.     // returns true or false
  374.     function _pconnect($argHostname$argUsername$argPassword$argDatabasename)
  375.     {
  376.         if (!emptyempty($this->port)) $argHostname .= ":".$this->port;
  377.         
  378.         if (ADODB_PHPVER >= 0x4300)
  379.             $this->_connectionID = mysql_pconnect($argHostname,$argUsername,$argPassword,$this->clientFlags);
  380.         else
  381.             $this->_connectionID = mysql_pconnect($argHostname,$argUsername,$argPassword);
  382.         if ($this->_connectionID === false) return false;
  383.        
  384.         //new row -begin           
  385.         if ($this->_isSupportUtf8() && $argDatabasename) {
  386.             $dbEncoding = $this->_getDbDefaultEncoding($argDatabasename);
  387.             if ($dbEncoding) {
  388.                 mysql_query("SET NAMES $dbEncoding"$this->_connectionID);
  389.             }
  390.         }
  391.         
  392.            // trun off strict mode
  393.         mysql_query("SET sql_mode = '';");
  394.        
  395.         //new row -end       
  396.         if ($this->autoRollback) $this->RollbackTrans();
  397.         if ($argDatabasenamereturn $this->SelectDB($argDatabasename);
  398.         return true;    
  399.     }
  400.     
  401.     function _nconnect($argHostname$argUsername$argPassword$argDatabasename)
  402.     {
  403.         $this->forceNewConnect = true;
  404.         return $this->_connect($argHostname$argUsername$argPassword$argDatabasename);
  405.     }
  406.     
  407.     function &MetaColumns($table
  408.     {
  409.         $this->_findschema($table,$schema);
  410.         if ($schema) {
  411.             $dbName = $this->database;
  412.             $this->SelectDB($schema);
  413.         }
  414.         global $ADODB_FETCH_MODE;
  415.         $save = $ADODB_FETCH_MODE;
  416.         $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  417.         
  418.         if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
  419.         $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
  420.         
  421.         if ($schema) {
  422.             $this->SelectDB($dbName);
  423.         }
  424.         
  425.         if (isset($savem)) $this->SetFetchMode($savem);
  426.         $ADODB_FETCH_MODE = $save;
  427.         if (!is_object($rs)) {
  428.             $false = false;
  429.             return $false;
  430.         }
  431.             
  432.         $retarr = array();
  433.         while (!$rs->EOF){
  434.             $fld = new ADOFieldObject();
  435.             $fld->name = $rs->fields[0];
  436.             $type = $rs->fields[1];
  437.             
  438.             // split type into type(length):
  439.             $fld->scale = null;
  440.             if (preg_match("/^(.+)/((/d+),(/d+)/"$type$query_array)) {
  441.                 $fld->type = $query_array[1];
  442.                 $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
  443.                 $fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1;
  444.             } elseif (preg_match("/^(.+)/((/d+)/"$type$query_array)) {
  445.                 $fld->type = $query_array[1];
  446.                 $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
  447.             } elseif (preg_match("/^(enum)/((.*)/)$/i"$type$query_array)) {
  448.                 $fld->type = $query_array[1];
  449.                 $arr = explode(",",$query_array[2]);
  450.                 $fld->enums = $arr;
  451.                 $zlen = max(array_map("strlen",$arr)) - 2; // PHP >= 4.0.6
  452.                 $fld->max_length = ($zlen > 0) ? $zlen : 1;
  453.             } else {
  454.                 $fld->type = $type;
  455.                 $fld->max_length = -1;
  456.             }
  457.             $fld->not_null = ($rs->fields[2] != 'YES');
  458.             $fld->primary_key = ($rs->fields[3] == 'PRI');
  459.             $fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false);
  460.             $fld->binary = (strpos($type,'blob') !== false);
  461.             $fld->unsigned = (strpos($type,'unsigned') !== false);
  462.                 
  463.             if (!$fld->binary) {
  464.                 $d = $rs->fields[4];
  465.                 if ($d != '' && $d != 'NULL') {
  466.                     $fld->has_default = true;
  467.                     $fld->default_value = $d;
  468.                 } else {
  469.                     $fld->has_default = false;
  470.                 }
  471.             }
  472.             
  473.             if ($save == ADODB_FETCH_NUM) {
  474.                 $retarr[] = $fld;
  475.             } else {
  476.                 $retarr[strtoupper($fld->name)] = $fld;
  477.             }
  478.                 $rs->MoveNext();
  479.             }
  480.         
  481.             $rs->Close();
  482.             return $retarr
  483.     }
  484.         
  485.     // returns true or false
  486.     function SelectDB($dbName
  487.     {
  488.         $this->database = $dbName;
  489.         $this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions
  490.         if ($this->_connectionID) {
  491.             return @mysql_select_db($dbName,$this->_connectionID);      
  492.         }
  493.         else return false;  
  494.     }
  495.     
  496.     // parameters use PostgreSQL convention, not MySQL
  497.     function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs=0)
  498.     {
  499.         $offsetStr =($offset>=0) ? ((integer)$offset)."," : '';
  500.         // jason judge, see http://phplens.com/lens/lensforum/msgs.php?id=9220
  501.         if ($nrows < 0) $nrows = '18446744073709551615'
  502.         
  503.         if ($secs)
  504.             $rs =& $this->CacheExecute($secs,$sql." LIMIT $offsetStr".((integer)$nrows),$inputarr);
  505.         else
  506.             $rs =& $this->Execute($sql." LIMIT $offsetStr".((integer)$nrows),$inputarr);
  507.         return $rs;
  508.     }
  509.     
  510.     // returns queryID or false
  511.     function _query($sql,$inputarr)
  512.     {
  513.     //global $ADODB_COUNTRECS;
  514.         //if($ADODB_COUNTRECS) 
  515.         return mysql_query($sql,$this->_connectionID);
  516.         //else return @mysql_unbuffered_query($sql,$this->_connectionID); // requires PHP >= 4.0.6
  517.     }
  518.     /*  Returns: the last error message from previous database operation    */  
  519.     function ErrorMsg() 
  520.     {
  521.     
  522.         if ($this->_logsql) return $this->_errorMsg;
  523.         if (emptyempty($this->_connectionID)) $this->_errorMsg = @mysql_error();
  524.         else $this->_errorMsg = @mysql_error($this->_connectionID);
  525.         return $this->_errorMsg;
  526.     }
  527.     
  528.     /*  Returns: the last error number from previous database operation */  
  529.     function ErrorNo() 
  530.     {
  531.         if ($this->_logsql) return $this->_errorCode;
  532.         if (emptyempty($this->_connectionID))  return @mysql_errno();
  533.         else return @mysql_errno($this->_connectionID);
  534.     }
  535.     
  536.     // returns true or false
  537.     function _close()
  538.     {
  539.         @mysql_close($this->_connectionID);
  540.         $this->_connectionID = false;
  541.     }
  542.     
  543.     /*
  544.     * Maximum size of C field
  545.     */
  546.     function CharMax()
  547.     {
  548.         return 255; 
  549.     }
  550.     
  551.     /*
  552.     * Maximum size of X field
  553.     */
  554.     function TextMax()
  555.     {
  556.         return 4294967295; 
  557.     }
  558.     
  559.     // "Innox - Juan Carlos Gonzalez" <jgonzalez#innox.com.mx>
  560.     function MetaForeignKeys( $table$owner = FALSE, $upper = FALSE, $associative = FALSE )
  561.      {
  562.          if ( !emptyempty($owner) ) {
  563.             $table = "$owner.$table";
  564.          }
  565.          $a_create_table = $this->getRow(sprintf('SHOW CREATE TABLE %s'$table));
  566.          if ($associative$create_sql = $a_create_table["Create Table"];
  567.          else $create_sql  = $a_create_table[1];
  568.          $matches = array();
  569.          if (!preg_match_all("/FOREIGN KEY /(`(.*?)`/) REFERENCES `(.*?)` /(`(.*?)`/)/"$create_sql$matches)) return false;
  570.          $foreign_keys = array();        
  571.          $num_keys = count($matches[0]);
  572.          for ( $i = 0;  $i < $num_keys;  $i ++ ) {
  573.              $my_field  = explode('`, `'$matches[1][$i]);
  574.              $ref_table = $matches[2][$i];
  575.              $ref_field = explode('`, `'$matches[3][$i]);
  576.              if ( $upper ) {
  577.                  $ref_table = strtoupper($ref_table);
  578.              }
  579.              $foreign_keys[$ref_table] = array();
  580.              $num_fields = count($my_field);
  581.              for ( $j = 0;  $j < $num_fields;  $j ++ ) {
  582.                  if ( $associative ) {
  583.                      $foreign_keys[$ref_table][$ref_field[$j]] = $my_field[$j];
  584.                  } else {
  585.                      $foreign_keys[$ref_table][] = "{$my_field[$j]}={$ref_field[$j]}";
  586.                  }
  587.              }
  588.          }
  589.          
  590.          return  $foreign_keys;
  591.      }
  592.  //add new function - begin  
  593. function _isSupportUtf8() {
  594.         // check mysql version first. Version lower than 4.1 doesn't support utf8
  595.         $serverVersion = mysql_get_server_info($this->_connectionID);
  596.         $version = explode('.'$serverVersion);
  597.         if ($version[0] < 4) return false;
  598.         if ( ($version[0] == 4) && ($version[1] == 0) ) return false;
  599.         
  600.         // check if utf8 support was compiled in
  601.         $result = mysql_query("SHOW CHARACTER SET like 'utf8'"$this->_connectionID);
  602.         if (mysql_num_rows($result) > 0) {
  603.             return true;
  604.         }
  605.         return false;
  606.     }
  607.     
  608.     function _getDbDefaultEncoding($argDatabasename){
  609.         if (!$argDatabasename) {
  610.             return false;
  611.         }
  612.         
  613.         // We use a SHOW CREATE DATABASE command to show the original
  614.         // SQL character set when DB was created.
  615.         $result = mysql_query("SHOW CREATE DATABASE `$argDatabasename`"$this->_connectionID);
  616.         if (mysql_num_rows($result) < 0 ) {
  617.             // The specified db name is wrong!
  618.             return false;
  619.         }
  620.         $dbInfo = mysql_fetch_row($result);
  621.         $pattern = '/40100 DEFAULT CHARACTER SET (/w+) /';
  622.         if ( (preg_match($pattern$dbInfo[1], $match) > 0) ) {
  623.             return $match[1];
  624.         }
  625.         return false;
  626.     }
  627.     //add new function - end
  628. }
  629.     
  630. /*--------------------------------------------------------------------------------------
  631.      Class Name: Recordset
  632. --------------------------------------------------------------------------------------*/
  633. class ADORecordSet_mysql extends ADORecordSet{  
  634.     
  635.     var $databaseType = "mysql";
  636.     var $canSeek = true;
  637.     
  638.     function ADORecordSet_mysql($queryID,$mode=false) 
  639.     {
  640.         if ($mode === false) { 
  641.             global $ADODB_FETCH_MODE;
  642.             $mode = $ADODB_FETCH_MODE;
  643.         }
  644.         switch ($mode)
  645.         {
  646.         case ADODB_FETCH_NUM: $this->fetchMode = MYSQL_NUM; break;
  647.         case ADODB_FETCH_ASSOC:$this->fetchMode = MYSQL_ASSOC; break;
  648.         case ADODB_FETCH_DEFAULT:
  649.         case ADODB_FETCH_BOTH:
  650.         default:
  651.             $this->fetchMode = MYSQL_BOTH; break;
  652.         }
  653.         $this->adodbFetchMode = $mode;
  654.         $this->ADORecordSet($queryID);  
  655.     }
  656.     
  657.     function _initrs()
  658.     {
  659.     //GLOBAL $ADODB_COUNTRECS;
  660.     //  $this->_numOfRows = ($ADODB_COUNTRECS) ? @mysql_num_rows($this->_queryID):-1;
  661.         $this->_numOfRows = @mysql_num_rows($this->_queryID);
  662.         $this->_numOfFields = @mysql_num_fields($this->_queryID);
  663.     }
  664.     
  665.     function &FetchField($fieldOffset = -1) 
  666.     {   
  667.         if ($fieldOffset != -1) {
  668.             $o = @mysql_fetch_field($this->_queryID, $fieldOffset);
  669.             $f = @mysql_field_flags($this->_queryID,$fieldOffset);
  670.             $o->max_length = @mysql_field_len($this->_queryID,$fieldOffset); // suggested by: Jim Nicholson (jnich@att.com)
  671.             //$o->max_length = -1; // mysql returns the max length less spaces -- so it is unrealiable
  672.             $o->binary = (strpos($f,'binary')!== false);
  673.         }
  674.         else if ($fieldOffset == -1) {  /*  The $fieldOffset argument is not provided thus its -1   */
  675.             $o = @mysql_fetch_field($this->_queryID);
  676.         $o->max_length = @mysql_field_len($this->_queryID); // suggested by: Jim Nicholson (jnich@att.com)
  677.         //$o->max_length = -1; // mysql returns the max length less spaces -- so it is unrealiable
  678.         }
  679.             
  680.         return $o;
  681.     }
  682.     function &GetRowAssoc($upper=true)
  683.     {
  684.         if ($this->fetchMode == MYSQL_ASSOC && !$upperreturn $this->fields;
  685.         $row =& ADORecordSet::GetRowAssoc($upper);
  686.         return $row;
  687.     }
  688.     
  689.     /* Use associative array to get fields array */
  690.     function Fields($colname)
  691.     {   
  692.         // added @ by "Michael William Miller" <mille562@pilot.msu.edu>
  693.         if ($this->fetchMode != MYSQL_NUM) return @$this->fields[$colname];
  694.         
  695.         if (!$this->bind) {
  696.             $this->bind = array();
  697.             for ($i=0; $i < $this->_numOfFields; $i++) {
  698.                 $o = $this->FetchField($i);
  699.                 $this->bind[strtoupper($o->name)] = $i;
  700.             }
  701.         }
  702.          return $this->fields[$this->bind[strtoupper($colname)]];
  703.     }
  704.     
  705.     function _seek($row)
  706.     {
  707.         if ($this->_numOfRows == 0) return false;
  708.         return @mysql_data_seek($this->_queryID,$row);
  709.     }
  710.     
  711.     function MoveNext()
  712.     {
  713.         //return adodb_movenext($this);
  714.         //if (defined('ADODB_EXTENSION')) return adodb_movenext($this);
  715.         if (@$this->fields = mysql_fetch_array($this->_queryID,$this->fetchMode)) {
  716.             $this->_currentRow += 1;
  717.             return true;
  718.         }
  719.         if (!$this->EOF) {
  720.             $this->_currentRow += 1;
  721.             $this->EOF = true;
  722.         }
  723.         return false;
  724.     }
  725.     
  726.     function _fetch()
  727.     {
  728.         $this->fields =  @mysql_fetch_array($this->_queryID,$this->fetchMode);
  729.         return is_array($this->fields);
  730.     }
  731.     
  732.     function _close() {
  733.         @mysql_free_result($this->_queryID);    
  734.         $this->_queryID = false;    
  735.     }
  736.     
  737.     function MetaType($t,$len=-1,$fieldobj=false)
  738.     {
  739.         if (is_object($t)) {
  740.             $fieldobj = $t;
  741.             $t = $fieldobj->type;
  742.             $len = $fieldobj->max_length;
  743.         }
  744.         
  745.         $len = -1; // mysql max_length is not accurate
  746.         switch (strtoupper($t)) {
  747.         case 'STRING'
  748.         case 'CHAR':
  749.         case 'VARCHAR'
  750.         case 'TINYBLOB'
  751.         case 'TINYTEXT'
  752.         case 'ENUM'
  753.         case 'SET'
  754.             if ($len <= $this->blobSize) return 'C';
  755.             
  756.         case 'TEXT':
  757.         case 'LONGTEXT'
  758.         case 'MEDIUMTEXT':
  759.             return 'X';
  760.             
  761.         // php_mysql extension always returns 'blob' even if 'text'
  762.         // so we have to check whether binary...
  763.         case 'IMAGE':
  764.         case 'LONGBLOB'
  765.         case 'BLOB':
  766.         case 'MEDIUMBLOB':
  767.             return !emptyempty($fieldobj->binary) ? 'B' : 'X';
  768.             
  769.         case 'YEAR':
  770.         case 'DATE'return 'D';
  771.         
  772.         case 'TIME':
  773.         case 'DATETIME':
  774.         case 'TIMESTAMP'return 'T';
  775.         
  776.         case 'INT'
  777.         case 'INTEGER':
  778.         case 'BIGINT':
  779.         case 'TINYINT':
  780.         case 'MEDIUMINT':
  781.         case 'SMALLINT'
  782.             
  783.             if (!emptyempty($fieldobj->primary_key)) return 'R';
  784.             else return 'I';
  785.         
  786.         defaultreturn 'N';
  787.         }
  788.     }
  789. }
  790. class ADORecordSet_ext_mysql extends ADORecordSet_mysql {   
  791.     function ADORecordSet_ext_mysql($queryID,$mode=false) 
  792.     {
  793.         if ($mode === false) { 
  794.             global $ADODB_FETCH_MODE;
  795.             $mode = $ADODB_FETCH_MODE;
  796.         }
  797.         switch ($mode)
  798.         {
  799.         case ADODB_FETCH_NUM: $this->fetchMode = MYSQL_NUM; break;
  800.         case ADODB_FETCH_ASSOC:$this->fetchMode = MYSQL_ASSOC; break;
  801.         case ADODB_FETCH_DEFAULT:
  802.         case ADODB_FETCH_BOTH:
  803.         default:
  804.         $this->fetchMode = MYSQL_BOTH; break;
  805.         }
  806.         $this->adodbFetchMode = $mode;
  807.         $this->ADORecordSet($queryID);
  808.     }
  809.     
  810.     function MoveNext()
  811.     {
  812.         return @adodb_movenext($this);
  813.     }
  814. }
  815. }
  816. ?>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值