请大家提出宝贵意见和建议,谢谢!
<?php
/*
* @filename:mysql.class.php
* @author:forest
* @email:chaizuxue@163.com
* @create:2006-10-24
* @environment:PHP4 or PHP5
* @copyright:Copyright 2006 forest
*
-- 数据库: `test`
--
-- --------------------------------------------------------
--
-- 表的结构 `administrator`
--
CREATE TABLE `administrator` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(50) NOT NULL default '',
`password` varchar(50) NOT NULL default '',
`groupid` int(11) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM COMMENT='后台管理' AUTO_INCREMENT=5 ;
--
-- 导出表中的数据 `administrator`
--
INSERT INTO `administrator` VALUES (1, 'test', 'test', 1);
INSERT INTO `administrator` VALUES (2, 'administratorer', 'administratorer', 1);
INSERT INTO `administrator` VALUES (3, 'administratorer', 'administratorer', 1);
INSERT INTO `administrator` VALUES (4, 'user', '123456', 0);
* Example:
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM administrator";
$result=$mysql->execute($sql);
print_r($result);
$mysql->close($mysql->link);
*/
class mysql
{
var $hostname,$username,$password,$database,$result;
var $pconnect=false;
var $debug=false;
var $link;
var $record=array();
var $error,$errno;
function mysql($hostname,$username,$password,$database)
{
$this->set("hostname",$hostname);
$this->set("username",$username);
$this->set("password",$password);
$this->set("database",$database);
$this->connect_db();
$this->select_db();
}
function __construct($hostname="localhost",$username,$password,$database)
{
$this->set("hostname",$hostname);
$this->set("username",$username);
$this->set("password",$password);
$this->set("database",$database);
$this->connect_db();
$this->select_db();
}
function set($var,$value)
{
$this->$var=$value;
}
function connect_db($hostname="",$username="",$password="")
{
if(!empty($hostname)) $this->set("hostname",$hostname);
if(!empty($username)) $this->set("username",$username);
if(!empty($password)) $this->set("password",$password);
if(!$this->link)
{
if($this->pconnect)
$this->link=mysql_pconnect($this->hostname,$this->username,$this->password);
else
$this->link=mysql_connect($this->hostname,$this->username,$this->password);
}
else die("Mysql Connect Error in ".__FUNCTION__."():".mysql_errno().":".mysql_error());
return $this->link;
}
function select_db()
{
mysql_select_db($this->database,$this->link) or die(mysql_error());
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
/*调用方法:
include "mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
$rel=$mysql->query($sql);
$mysql->free_result();
*/
function query($sql)
{
if($this->result=@mysql_query($sql,$this->link))
return $this->result;
else
{
echo "<center><br>SQL语句错误:<font color=red>$sql</font><br>错误信息:".mysql_error();
return false;
}
}
//------------------------------------------------------------------------------------
/*------------------------------------------------------------------------------------
/*调用方法:
include "mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
$rel=$mysql->execute($sql);
print_r($rel);
$mysql->free_result();
*/
function execute($sql,$style="array")
{
$this->query($sql);
if(!empty($this->record))
$this->record=array();
$i=0;
if($style=="array")
{
while ($temp=@mysql_fetch_array($this->result))
{
$this->record[$i]=$temp;
$i++;
}
}
else
{
while ($temp=@mysql_fetch_object($this->result))
{
$this->record[$i]=$temp;
$i++;
}
}
unset($i);
unset($temp);
return $this->record;
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:返回根据所取得的行生成的对象
/*调用方法:
include "mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
$obj=$mysql->fetch_object($sql);
echo $obj->username;
$mysql->free_result();
*/
function fetch_object($sql)
{
$this->result=$this->query($sql);
while ($obj=mysql_fetch_object($this->result))
{
$object=$obj;
}
return $object;
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:返回最后一条记录集
/*调用方法:
include "mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
$array=$mysql->fetch_one_array($sql);
print_r($array);
$mysql->free_result();
*/
function fetch_one_array($sql)
{
return @mysql_fetch_array($this->query($sql));
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:返回最后一条记录集
/*调用方法:
include "mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
$array=$mysql->fetch_array($sql);
print_r($array);
$mysql->free_result();
*/
function fetch_array($sql)
{
$this->result=@mysql_query($sql);
while ($row=@mysql_fetch_array($this->result))
{
$array[]=$row;
}
return $array;
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:取得最近一次与link关联的INSERT,UPDATE或DELETE查询所影响的记录行数
/*调用方法:
include "mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
$rel=$mysql->fetch_array($sql);
echo $mysql->affected_rows($mysql->link);
*/
function affected_rows()
{
return mysql_affected_rows($this->link);
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:取得数据库中记录总数
/*调用方法:
include "mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
echo $mysql->num_rows($sql);
*/
function num_rows($sql)
{
return @mysql_num_rows($this->query($sql));
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:返回结果集中字段的数目
/*调用方法:
include"mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
echo $mysql->num_fields($sql);
*/
function num_fields($sql)
{
return @mysql_num_fields($this->query($sql));
}
//------------------------------------------------------------------------------------
//*功能描述:取得上一步INSERT操作产生的ID
/*调用方法:
include"mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="INSERT INTO `administrator` VALUES ('', 'test', 'test', 0)";
$mysql->query($sql);
echo $mysql->insert_id();
*/
function insert_id()
{
return mysql_insert_id($this->link);
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:取得MySQL客户端信息
/*调用方法:
include"mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
echo $mysql->get_client_info();
*/
function get_client_info()
{
return mysql_get_client_info();
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:取得MySQL主机信息
/*调用方法:
include"mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
echo $mysql->get_host_info();
*/
function get_host_info()
{
return mysql_get_host_info();
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:取得MySQL协议信息
/*调用方法:
include"mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
echo $mysql->get_proto_info();
*/
function get_proto_info()
{
return mysql_get_proto_info();
}
//------------------------------------------------------------------------------------
//*功能描述:取得MySQL服务器信息
/*调用方法:
include"mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
echo $mysql->get_server_info();
*/
function get_server_info()
{
return mysql_get_server_info();
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:取得数据库中记录总数
/*调用方法:
include"mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
$rel=$mysql->execute($sql);
$mysql->free_result();
unset($rel);
print_r($rel);
print_r($mysql->result);
*/
function free_result()
{
return is_resource($this->result) ? mysql_free_result($this->result) : true;
}
function get_error()
{
$this->error=mysql_error($this->link);
return $this->error;
}
function get_errno()
{
$this->errno=mysql_errno($this->link);
return $this->errno;
}
function close_db()
{
$this->free_result();
mysql_close($this->link) or die(mysql_error());
}
function __destruct()
{
$this->close_db();
$this->set("hostname","");
$this->set("username","");
$this->set("password","");
$this->set("database","");
}
function insert($table,$field)
{
$temp=explode(',',$field);
$insert='';
for($i=0;$i<count($temp);$i++)
{
$insert.="'".$_POST[$temp[$i]]."',";
}
$insert=substr($insert,0,-1);
$sql="INSERT INTO ".$table." (".$field.") VALUES (".$insert.")";
$this->query($sql);
}
function update($table,$set,$condition)
{
$sql="UPDATE ".$table." SET ".$set." WHERE ".$condition;
$this->query($sql);
}
function delete($table,$condition)
{
$sql="DELETE FROM ".$table." WHERE ".$condition;
$this->query($sql);
}
}
?>
<?php
/*
* @filename:mysql.class.php
* @author:forest
* @email:chaizuxue@163.com
* @create:2006-10-24
* @environment:PHP4 or PHP5
* @copyright:Copyright 2006 forest
*
-- 数据库: `test`
--
-- --------------------------------------------------------
--
-- 表的结构 `administrator`
--
CREATE TABLE `administrator` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(50) NOT NULL default '',
`password` varchar(50) NOT NULL default '',
`groupid` int(11) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM COMMENT='后台管理' AUTO_INCREMENT=5 ;
--
-- 导出表中的数据 `administrator`
--
INSERT INTO `administrator` VALUES (1, 'test', 'test', 1);
INSERT INTO `administrator` VALUES (2, 'administratorer', 'administratorer', 1);
INSERT INTO `administrator` VALUES (3, 'administratorer', 'administratorer', 1);
INSERT INTO `administrator` VALUES (4, 'user', '123456', 0);
* Example:
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM administrator";
$result=$mysql->execute($sql);
print_r($result);
$mysql->close($mysql->link);
*/
class mysql
{
var $hostname,$username,$password,$database,$result;
var $pconnect=false;
var $debug=false;
var $link;
var $record=array();
var $error,$errno;
function mysql($hostname,$username,$password,$database)
{
$this->set("hostname",$hostname);
$this->set("username",$username);
$this->set("password",$password);
$this->set("database",$database);
$this->connect_db();
$this->select_db();
}
function __construct($hostname="localhost",$username,$password,$database)
{
$this->set("hostname",$hostname);
$this->set("username",$username);
$this->set("password",$password);
$this->set("database",$database);
$this->connect_db();
$this->select_db();
}
function set($var,$value)
{
$this->$var=$value;
}
function connect_db($hostname="",$username="",$password="")
{
if(!empty($hostname)) $this->set("hostname",$hostname);
if(!empty($username)) $this->set("username",$username);
if(!empty($password)) $this->set("password",$password);
if(!$this->link)
{
if($this->pconnect)
$this->link=mysql_pconnect($this->hostname,$this->username,$this->password);
else
$this->link=mysql_connect($this->hostname,$this->username,$this->password);
}
else die("Mysql Connect Error in ".__FUNCTION__."():".mysql_errno().":".mysql_error());
return $this->link;
}
function select_db()
{
mysql_select_db($this->database,$this->link) or die(mysql_error());
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
/*调用方法:
include "mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
$rel=$mysql->query($sql);
$mysql->free_result();
*/
function query($sql)
{
if($this->result=@mysql_query($sql,$this->link))
return $this->result;
else
{
echo "<center><br>SQL语句错误:<font color=red>$sql</font><br>错误信息:".mysql_error();
return false;
}
}
//------------------------------------------------------------------------------------
/*------------------------------------------------------------------------------------
/*调用方法:
include "mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
$rel=$mysql->execute($sql);
print_r($rel);
$mysql->free_result();
*/
function execute($sql,$style="array")
{
$this->query($sql);
if(!empty($this->record))
$this->record=array();
$i=0;
if($style=="array")
{
while ($temp=@mysql_fetch_array($this->result))
{
$this->record[$i]=$temp;
$i++;
}
}
else
{
while ($temp=@mysql_fetch_object($this->result))
{
$this->record[$i]=$temp;
$i++;
}
}
unset($i);
unset($temp);
return $this->record;
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:返回根据所取得的行生成的对象
/*调用方法:
include "mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
$obj=$mysql->fetch_object($sql);
echo $obj->username;
$mysql->free_result();
*/
function fetch_object($sql)
{
$this->result=$this->query($sql);
while ($obj=mysql_fetch_object($this->result))
{
$object=$obj;
}
return $object;
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:返回最后一条记录集
/*调用方法:
include "mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
$array=$mysql->fetch_one_array($sql);
print_r($array);
$mysql->free_result();
*/
function fetch_one_array($sql)
{
return @mysql_fetch_array($this->query($sql));
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:返回最后一条记录集
/*调用方法:
include "mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
$array=$mysql->fetch_array($sql);
print_r($array);
$mysql->free_result();
*/
function fetch_array($sql)
{
$this->result=@mysql_query($sql);
while ($row=@mysql_fetch_array($this->result))
{
$array[]=$row;
}
return $array;
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:取得最近一次与link关联的INSERT,UPDATE或DELETE查询所影响的记录行数
/*调用方法:
include "mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
$rel=$mysql->fetch_array($sql);
echo $mysql->affected_rows($mysql->link);
*/
function affected_rows()
{
return mysql_affected_rows($this->link);
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:取得数据库中记录总数
/*调用方法:
include "mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
echo $mysql->num_rows($sql);
*/
function num_rows($sql)
{
return @mysql_num_rows($this->query($sql));
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:返回结果集中字段的数目
/*调用方法:
include"mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
echo $mysql->num_fields($sql);
*/
function num_fields($sql)
{
return @mysql_num_fields($this->query($sql));
}
//------------------------------------------------------------------------------------
//*功能描述:取得上一步INSERT操作产生的ID
/*调用方法:
include"mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="INSERT INTO `administrator` VALUES ('', 'test', 'test', 0)";
$mysql->query($sql);
echo $mysql->insert_id();
*/
function insert_id()
{
return mysql_insert_id($this->link);
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:取得MySQL客户端信息
/*调用方法:
include"mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
echo $mysql->get_client_info();
*/
function get_client_info()
{
return mysql_get_client_info();
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:取得MySQL主机信息
/*调用方法:
include"mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
echo $mysql->get_host_info();
*/
function get_host_info()
{
return mysql_get_host_info();
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:取得MySQL协议信息
/*调用方法:
include"mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
echo $mysql->get_proto_info();
*/
function get_proto_info()
{
return mysql_get_proto_info();
}
//------------------------------------------------------------------------------------
//*功能描述:取得MySQL服务器信息
/*调用方法:
include"mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
echo $mysql->get_server_info();
*/
function get_server_info()
{
return mysql_get_server_info();
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//*功能描述:取得数据库中记录总数
/*调用方法:
include"mysql.class.php";
$mysql=new mysql("localhost","root","root","test");
$sql="SELECT * FROM `administrator`";
$rel=$mysql->execute($sql);
$mysql->free_result();
unset($rel);
print_r($rel);
print_r($mysql->result);
*/
function free_result()
{
return is_resource($this->result) ? mysql_free_result($this->result) : true;
}
function get_error()
{
$this->error=mysql_error($this->link);
return $this->error;
}
function get_errno()
{
$this->errno=mysql_errno($this->link);
return $this->errno;
}
function close_db()
{
$this->free_result();
mysql_close($this->link) or die(mysql_error());
}
function __destruct()
{
$this->close_db();
$this->set("hostname","");
$this->set("username","");
$this->set("password","");
$this->set("database","");
}
function insert($table,$field)
{
$temp=explode(',',$field);
$insert='';
for($i=0;$i<count($temp);$i++)
{
$insert.="'".$_POST[$temp[$i]]."',";
}
$insert=substr($insert,0,-1);
$sql="INSERT INTO ".$table." (".$field.") VALUES (".$insert.")";
$this->query($sql);
}
function update($table,$set,$condition)
{
$sql="UPDATE ".$table." SET ".$set." WHERE ".$condition;
$this->query($sql);
}
function delete($table,$condition)
{
$sql="DELETE FROM ".$table." WHERE ".$condition;
$this->query($sql);
}
}
?>