最近写的一个MySQL操作类(含调用方法),发上来与大家共享
来源:互联网 发布:javascript 变量 编辑:程序博客网 时间:2024/05/16 09:25
请大家提出宝贵意见和建议,谢谢!
<?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);
}
}
?>
- 最近写的一个MySQL操作类(含调用方法),发上来与大家共享
- 自己用面向对象写了一个图片上传类,发上来大家瞧瞧?
- 发现一个疑似IE与mshta的bug,发上来大家看下
- 写了一个比较简单的会员注册页面,带会员已经存在验证,发上来大家给点建议
- 花了差不多一个半小时写了一个基于C++的链表程序,现在发上来,大家多给点意见啊
- 早上学习了一个smarty局部刷新的例子发上来大家看看,过两天我会把它与数据库相互结合起来写成功发过来的
- 最近写的C++分数类,与大家一起学习
- 最近项目中需要在指定的字符串中取出数字字符,找了好多但都是词不达意,索性自己写了个方法,与大家共享!
- 今天写了一个通过switch进行添加,修过,显示,删除程序,发上来大家看看,哈哈,并且用了smarty模版
- 本人最近写的Ajax留言板,与大家分享!
- greybox的使用方法(感觉不错,就发上来给大家看看)
- 偶然又发现了一套Graphis的图,发上来给大家看看
- php写一个操作mysql的类
- 最近要写 发短信的代码,哈哈一点心得,大家共享
- 最近写了一个HTTP代理服务器, 有些心得, 与大家分享
- 突然在电脑上发现自己很久以前写的《科学美学与审美创造》通识课的论文,于是发上来了,^_^
- 两天没写bg 在学习购物车类 学会发上来
- 最近写的一个网站的聚合的实现方法,
- 一些常用的php函数
- 高效程序员应该养成的七个习惯
- 采用p6spy完整显示hibernate的SQL语句
- MySQL数据库类的定义
- C++的学习感想
- 最近写的一个MySQL操作类(含调用方法),发上来与大家共享
- outerHTML、innerHTML以及innerTEXT的区别
- VVVV
- 提升交谈能力的三个技巧(IT经理人)
- Smarty教程之二:程序设计部分
- php4中有关session的函数
- 时机的把握!
- 文本超过一定长度自动省略
- Smarty实例教程之三:----实例篇( 使用PHP内置MYSQL函数)