分类:
2009-12-30 18:15:38
/**
* 模块说明:公用函数库
* 包括
* dump函数
* Mysql类
*/
/**
* 从FLEA中复制过来,用来输出变量以调试
*
* @param 任意类型 $vars
* @param unknown_type $label
* @param unknown_type $return
* @return 无
*/
function dump($vars, $label = '', $return = false,$native='gb2312'){
if (ini_get('html_errors')) {
$content = "\n"
;
if ($label != '') {
$content .= "{$label} :\n";
}
$content .= htmlspecialchars(print_r($vars, true));
$content .= "\n\n";
} else {
$content = $label . " :\n" . print_r($vars, true);
}
if ($return) { return $content; }
echo "\n";
echo mb_convert_encoding($content,$native,'utf-8');
return null;
}
require_once('ui.php');//包含多语言处理
//本程序用到的数据库类
class Mysql{
private $server;
private $user;
private $password;
private $database;
private $port;
private $connect=null;
function InsertID(){
return mysql_insert_id($this->connect);
}
function Mysql($db){
$this->server=$db['host'];
$this->user=$db['login'];
$this->password=$db['password'];
$this->database=$db['database'];
$this->port=$db['port'];
$this->Connect();
}
//根据SQL语句返回一个字段的值
//用法:$field=SQLSingle($sql);
function Single($sql){
$this->Connect();
$result=mysql_query($sql,$this->connect);
$this->Test($result,$sql);
$data=mysql_fetch_array($result);
return $data[0];
}
//从一条SQL语句中返回一条记录中的多个字段
//用法:list($field1,$field2,...)=SQLMulti($sql,array('field1','field2',...));
function Multi($sql,$fields){
$this->Connect();
$result=mysql_query($sql,$this->connect);
$this->Test($result,$sql);
$data=mysql_fetch_array($result);
$a=array();
for($i=0;$i<count($fields);$i++)$a[$i]=$data[$fields[$i]];
return $a;
}
//从一条SQL语句中返回N条记录的同一列
function Column($sql){
$this->Connect();
$table=$this->Table($sql);
$return=array();
foreach($table as $row):
$return[]=$row[0];
endforeach;
return $return;
}
//从一条SQL语句中返回一个表格
function Table($sql){
$this->Connect();
$table=array();
$result=mysql_query($sql,$this->connect);
$this->Test($result,$sql);
while($data=mysql_fetch_array($result))
$table[]=$data;
return $table;
}
//返回SQL语句返回的数据行数
function Rows($sql){
$this->Connect();
return mysql_num_rows(mysql_query($sql,$this->connect));
}
//执行SQL语句
function Query($sql){
$this->Connect();
$return=mysql_query($sql,$this->connect);
$this->Test($return,$sql);
}
//连接数据库
function Connect(){
if($this->connect==null){
$this->connect=mysql_connect($this->server.":".$this->port,$this->user,$this->password);
if(!$this->connect){
echo _T::connectError."\n";
exit;
}
mysql_select_db($this->database);
mysql_query("set character set utf8");
}
}
//显示错误信息
function Test($retcode,$sql){
if(! $retcode ):
echo _T::sqlError.":$sql\n";
endif;
}
}
?>