【php开源代码栏目提醒】:网学会员为需要php开源代码的朋友们搜集整理了class.mysqldb.php相关资料,希望对各位网友有所帮助!
<?php
/**
* @author Jonathan Gotti <nathan at the-ring dot home
linux dot net>
* @copyleft (l) 2003-2004 Jonathan Gotti
* @package DB
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @subpackage MYSQL
* @changelog 2005-02-28 new method optimize
* 2004-12-03 now the associative_array_from_q2a_res method won't automaticly ksort the results
* 2004-12-02 use the show fields query in place of a select statement and add extended_info mode to the get_fields method
* 2004-11-26 first version
* @modifier kukukuan
* @bugfix fix magic_quotes_gpc (remove excessive slashes)
*/
/** class to deal with mysql databases */
class mysqldb{
/**array of error number and msgs*/
var $error;
/**the last error array*/
var $last_error;
/**resource mysql connection*/
var $conn;
/**Db hostname*/
var $host;
/**mysql username*/
var $user;
/**mysql pass
word*/
var $pass;
/**mysql selected database*/
var $dbname;
/**resource mysql db selected*/
var $db;
/** resource result handler*/
var $last_qres;
/**array of last query to array results*/
var $last_q2a_res;
var $beverbose = FALSE;
var $autoconnect = TRUE;
function mysqldb($dbname,$dbhost='localhost',$dbuser='root',$dbpass=''){ # most common config ?
$this->host = $dbhost;
$this->user = $dbuser;
$this->pass = $dbpass;
$this->dbname = $dbname;
$this->autoconnect= TRUE;
$this->open();
$this->beverbose = FALSE;
}
function open(){ # only for convenience and because backport of sqlitedb
return $this->check_conn('active');
}
function close(){
return $this->check_conn('kill');
}
function quote_smart($value){
// Stripslashes
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
// Quote if not integer
if (!is_numeric($value)) {
$value = my
sql_real_escape_string($value);
}
return $value;
}
/**
* Select the database to work on (it's the same as the use db command or
mysql_select_db function)
* @param string $dbname
* @return bool
*/
function select_db($dbname=null){
if(! ($dbname ||$this->dbname) )
return FALSE;
if($dbname)
$this->dbname = $dbname;
if(! $this->db = @mysql_select_db($this->dbname,$this->conn)){
$this->verbose("FATAL ERROR CAN'T CONNECT TO database ".$this->dbname);
$this->set_error();
return FALSE;
}else{
return $this->db;
}
}
/**
* check and activate db connection
* @param string $action (active, kill, check) active by default
*/
function check_conn($action = ''){
if(! $host = @mysql_get_host_info($this->conn)){
switch ($action){
case 'kill':
return $host;
break;
case 'check':
return $host;
break;
default:
case 'active':
if(! $this->conn = @mysql_connect($this->host,$this->user,$this->pass)){
$this->verbose("CONNECTION TO $this->host FAILED");
return FALSE;
}
$this->verbose("CONNECTION TO $this->host ESTABLISHED");
$this->select_db();
mysql_query("SET CHARACTER SET 'utf8'");
mysql_query("SET NAMES 'utf8'");
return @mysql_get_host_info($this->conn);
break;
}
}else{
switch($action){
case 'kill':
@mysql_close($this->conn);
$this->conn = $this->db = null;
return true;
break;
case 'check':
return $host;
break;
default:
case 'active':
return $host;
break;
}
}
}
/**
* send a select query to $table with arr $fields requested (all by default) and with arr $conditions
* sample conds array is array(0=>'field1 = field2','ORDER'=>'field desc','GROUP'=>'fld')
* @param string|array $Table
* @param string|array $fields
* @param string|array $conditions
* @param MYSQL_CONST $res_type MYSQL_ASSOC, MYSQL_NUM et MYSQL_BOTH
* @Return array | false
**/
function select_to_array($tables,$fields = '*', $conds = null,$result_type = MYSQL_ASSOC){
//we make the table list for the Q_str
if(! $tb_str = $this->array_to_str($tables))
return FALSE;
//we make the fields list for the Q_str
if(! $fld_str = $this->array_to_str($fields))
$fld_str = '*';
//now the WHERE str
if($conds)
$conds_str = $this->process_conds($conds);
$Q_str = "SELECT $fld_str FROM $tb_str $conds_str";
# echo "SQL : $Q_str\n;";
return $this->query_to_array($Q_str,$result_type);
}
/**
* Same as select_to_array but return only the first row.
* equal to $res = select_to_array followed by $res = $res[0];
* @see select_to_array for details
* @return array of fields
*/
function select_single_to_array($tables,$fields = '*', $conds = null,$result_type = MYSQL_ASSOC){
if(! $res = $this->select_to_array($tables,$fields,$conds,$result_type))
return FALSE;
return $res[0];
}
/**
* just a quick way to do