|
class dbClass{ //开始数据库类
var $username;
var $password;
var $database;
var $hostname;
var $link;
var $result;
function dbClass($username,$password,$database,$hostname="localhost"){
$this->username=$username;
$this->password=$password;
$this->database=$database;
$this->hostname=$hostname;
}
function connect(){ //这个函数用于连接数据库
if(!$this->link=mysql_connect($this->hostname,$this->username,$this->password))
$this->halt("Sorry,can not connect to database");
if($this->version() > '4.1') {
global $dbcharset,$charset;
if(!$dbcharset && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8'))) {
$dbcharset = str_replace('-', '', $charset);
}
if($dbcharset) {
mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary");
}
}
if($this->version() > '5.0.1') {
mysql_query("SET sql_mode=''");
}
return $this->link;
}
function select(){ //这个函数用于选择数据库
mysql_select_db($this->database,$this->link);
}
function query($sql){ //这个函数用于送出查询语句并返回结果,常用。
if($this->result=mysql_query($sql,$this->link)) return $this->result;
else {
$this->halt("SQL语句错误: <font color=red>$sql</font><br><br>错误信息: ".mysql_error());
return false;
}
}
/*
以下函数用于从结果取回数组,一般与 while()循环、$db->query($sql) 配合使用,例如:
$result=query("select * from mytable");
while($row=$db->getarray($result)){
echo "$row[id] ";
}
*/
function getarray($result){
return @mysql_fetch_array($result);
}
/*