PHP数据库操作类
时间:2008-10-22
来源:互联网
是PHPLIB的数据库操作类的扩展,用起来不错:
[code]<?php
/*
* 继承MyDd_MySql类.
*/
class MyDd_MySql extends DB_MYSQL {
/******************************
* 插入记录
* input parameter
: $table => 表名
: $field => 参数数组:$array['字段名'] = 值.
* out parameter
: ture or false
******************************/
function InsertDB($table, $field){
$Afield = $this->metadata($table);
$Sql = "INSERT INTO `$table`";
foreach($field as $key=>$value){
$fields .= "`$key`,";
foreach($Afield as $fname){
if($fname['name']==$key){
$Ftyle = $fname['type'];
break;
}
}
if($Ftyle=="string"||$Ftyle=="blob"||$Ftyle=="date") $values.="'$value',";
else $values.="$value,";
}
$fields = substr($fields, 0, -1);
$values = substr($values, 0, -1);
$Sql .= " (".$fields.")";
$Sql .= " VALUES (".$values.")";
//die($Sql);
return $this->query($Sql);
}
/******************************
* 删除记录
* input parameter
: $table => 表名
: $where => 删除条件
* out parameter
true or false
******************************/
function DeleteDB($table, $where='1') {
if(empty($table)) die("table is empty!");
$deleteSql = "DELETE FROM `$table` WHERE $where";
return $this->query($deleteSql);
}
/******************************
* 更新记录
* input parameter
: $table => 表名
: $field => 参数数组:$array['字段名'] = 值.
: $where => 更新条件
* out parameter
: ture or false
******************************/
function UpdateDB($table, $field, $where='1'){
$Afield = $this->metadata($table);
$Sql = "UPDATE `$table` SET";
if(is_array($field)){
foreach($field as $key => $var){
foreach($Afield as $fname){
if($fname['name']==$key){
if($fname['type']=="string"||$fname['type']=="blob"||$fname['type']=="date") $Sql.=" `$key`='$var',";
else $Sql.=" `$key`=$var,";
break;
}
}
}
$Sql = substr($Sql, 0, -1);
}else{
$Sql .= trim($field);
}
$Sql .= ' WHERE '.$where;
//die($Sql);
return $this->query($Sql);
}
/******************************
* 查询记录
* input parameter
: $table => 表名
: $field => 表字段
: $where => 查询条件
: $order => 排序方式
: $limit => 显示数目
* out parameter
: $array => 信息数组(二维数组)
******************************/
function SelectDB($table, $field='', $where='', $order='', $limit=''){
$Sql = "select ";
if(is_array($field)){
foreach ($field as $key){
$felids .= "`$key`,";
}
$felids = substr($felids, 0, -1);
}else{
$felids = "*";
}
$Sql .= $felids." from `".$table."` where 1";
if($where != '') $Sql .= " and ".$where;
if($order != '') $Sql .= " order by ".$order;
if($limit != '') $Sql .= " limit ".$limit;
//die($Sql);
$result = $this->query($Sql);
$array = array();
while($row = $this->fetch_array($result)){
$array[] = $row;
}
return $array;
}
/******************************
* 统计数据总数
* input parameter
: $table => 表名
* out parameter
: $countArr['num'] => 返回条件数据总数
******************************/
function CountData($table, $where='') {
$countSql = "SELECT count(*) as num FROM $table";
$countSql .= ($where == '') ? '' : " WHERE $where";
$countResult = $this->query($countSql);
$countArr = $this->fetch_array($countResult);
//die($countArr['num']);
return $countArr['num'];
}
/******************************
* 分页
* input parameter
: $pageNum => 每页显示记录数
: $table => 表名
: $where => 查询条件
* out parameter
: $limit => 返回用于第二次查询的SQL中的limit部分
******************************/
function LimitStr($table, $pageNum=10, $where='', $str='str', $view=true, $jump=true){
global $tpl;
//当前页码、每页显示数、开始记录
$page = is_numeric($_GET['page']) ? $_GET['page'] : 1;
if($page<1) $page = 1;
$start = ($page-1)*$pageNum;
$limit = "$start, $pageNum";
//记录总数
$total = $this->CountData($table, $where);
//分页字符串
$PageObj = new PAGE($total, $pageNum);
$pagestr = $PageObj->StartPage($str, $view, $jump);
$tpl->set_var("pagestr", $pagestr); //替换模板分页变量
//返回limt:$start,$pageNum
return $limit;
}
/*某一条记录*/
function RecordOne($table, $where='1', $field='*'){
$sql = 'select '.$field.' from '.$table.' where '.$where;
$this->query($sql);
$this->next_record();
return $this->Record;
}
/*获取最大ID号*/
function MaxID($table, $field='id'){
$sql = 'select MAX('.$field.') as MaxId from '.$table;
$this->query($sql);
$this->next_record();
return $this->Record['MaxId'];
}
/*记录集数组*/
function fetch_array($result){
return mysql_fetch_array($result, MYSQL_ASSOC);
}
/*设置数据库字符集*/
function SetNames($str){
mysql_query("set names '$str'", $this->Link_ID);
}
/*输出SQL语句,调试之用*/
function SQL($str){
die($str);
}
/*JS提示框*/
function forward($msg, $methd='', $url = ''){
$sStr = "<script language='javascript' type='text/javascript'>\n";
if($methd = 'href' && $url = '') die('forward funciton is wrong!');
$sStr .= " alert('$msg!'); \n";
switch ($methd){
case "href":
$sStr .= " location.href='".$url."'; \n";
break;
case "close":
$sStr .= " self.close(); \n";
break;
default:
$sStr .= " history.go(-1); \n";
}
$sStr .= "</script>";
die($sStr);
}
}//class End
?>[/code]
[code]<?php
/*
* 继承MyDd_MySql类.
*/
class MyDd_MySql extends DB_MYSQL {
/******************************
* 插入记录
* input parameter
: $table => 表名
: $field => 参数数组:$array['字段名'] = 值.
* out parameter
: ture or false
******************************/
function InsertDB($table, $field){
$Afield = $this->metadata($table);
$Sql = "INSERT INTO `$table`";
foreach($field as $key=>$value){
$fields .= "`$key`,";
foreach($Afield as $fname){
if($fname['name']==$key){
$Ftyle = $fname['type'];
break;
}
}
if($Ftyle=="string"||$Ftyle=="blob"||$Ftyle=="date") $values.="'$value',";
else $values.="$value,";
}
$fields = substr($fields, 0, -1);
$values = substr($values, 0, -1);
$Sql .= " (".$fields.")";
$Sql .= " VALUES (".$values.")";
//die($Sql);
return $this->query($Sql);
}
/******************************
* 删除记录
* input parameter
: $table => 表名
: $where => 删除条件
* out parameter
true or false
******************************/
function DeleteDB($table, $where='1') {
if(empty($table)) die("table is empty!");
$deleteSql = "DELETE FROM `$table` WHERE $where";
return $this->query($deleteSql);
}
/******************************
* 更新记录
* input parameter
: $table => 表名
: $field => 参数数组:$array['字段名'] = 值.
: $where => 更新条件
* out parameter
: ture or false
******************************/
function UpdateDB($table, $field, $where='1'){
$Afield = $this->metadata($table);
$Sql = "UPDATE `$table` SET";
if(is_array($field)){
foreach($field as $key => $var){
foreach($Afield as $fname){
if($fname['name']==$key){
if($fname['type']=="string"||$fname['type']=="blob"||$fname['type']=="date") $Sql.=" `$key`='$var',";
else $Sql.=" `$key`=$var,";
break;
}
}
}
$Sql = substr($Sql, 0, -1);
}else{
$Sql .= trim($field);
}
$Sql .= ' WHERE '.$where;
//die($Sql);
return $this->query($Sql);
}
/******************************
* 查询记录
* input parameter
: $table => 表名
: $field => 表字段
: $where => 查询条件
: $order => 排序方式
: $limit => 显示数目
* out parameter
: $array => 信息数组(二维数组)
******************************/
function SelectDB($table, $field='', $where='', $order='', $limit=''){
$Sql = "select ";
if(is_array($field)){
foreach ($field as $key){
$felids .= "`$key`,";
}
$felids = substr($felids, 0, -1);
}else{
$felids = "*";
}
$Sql .= $felids." from `".$table."` where 1";
if($where != '') $Sql .= " and ".$where;
if($order != '') $Sql .= " order by ".$order;
if($limit != '') $Sql .= " limit ".$limit;
//die($Sql);
$result = $this->query($Sql);
$array = array();
while($row = $this->fetch_array($result)){
$array[] = $row;
}
return $array;
}
/******************************
* 统计数据总数
* input parameter
: $table => 表名
* out parameter
: $countArr['num'] => 返回条件数据总数
******************************/
function CountData($table, $where='') {
$countSql = "SELECT count(*) as num FROM $table";
$countSql .= ($where == '') ? '' : " WHERE $where";
$countResult = $this->query($countSql);
$countArr = $this->fetch_array($countResult);
//die($countArr['num']);
return $countArr['num'];
}
/******************************
* 分页
* input parameter
: $pageNum => 每页显示记录数
: $table => 表名
: $where => 查询条件
* out parameter
: $limit => 返回用于第二次查询的SQL中的limit部分
******************************/
function LimitStr($table, $pageNum=10, $where='', $str='str', $view=true, $jump=true){
global $tpl;
//当前页码、每页显示数、开始记录
$page = is_numeric($_GET['page']) ? $_GET['page'] : 1;
if($page<1) $page = 1;
$start = ($page-1)*$pageNum;
$limit = "$start, $pageNum";
//记录总数
$total = $this->CountData($table, $where);
//分页字符串
$PageObj = new PAGE($total, $pageNum);
$pagestr = $PageObj->StartPage($str, $view, $jump);
$tpl->set_var("pagestr", $pagestr); //替换模板分页变量
//返回limt:$start,$pageNum
return $limit;
}
/*某一条记录*/
function RecordOne($table, $where='1', $field='*'){
$sql = 'select '.$field.' from '.$table.' where '.$where;
$this->query($sql);
$this->next_record();
return $this->Record;
}
/*获取最大ID号*/
function MaxID($table, $field='id'){
$sql = 'select MAX('.$field.') as MaxId from '.$table;
$this->query($sql);
$this->next_record();
return $this->Record['MaxId'];
}
/*记录集数组*/
function fetch_array($result){
return mysql_fetch_array($result, MYSQL_ASSOC);
}
/*设置数据库字符集*/
function SetNames($str){
mysql_query("set names '$str'", $this->Link_ID);
}
/*输出SQL语句,调试之用*/
function SQL($str){
die($str);
}
/*JS提示框*/
function forward($msg, $methd='', $url = ''){
$sStr = "<script language='javascript' type='text/javascript'>\n";
if($methd = 'href' && $url = '') die('forward funciton is wrong!');
$sStr .= " alert('$msg!'); \n";
switch ($methd){
case "href":
$sStr .= " location.href='".$url."'; \n";
break;
case "close":
$sStr .= " self.close(); \n";
break;
default:
$sStr .= " history.go(-1); \n";
}
$sStr .= "</script>";
die($sStr);
}
}//class End
?>[/code]
作者: tonlywang 发布时间: 2008-10-22
我还是比较喜欢使用把SQL写在外面的,改起来方便.
作者: gincn 发布时间: 2008-10-22
我发现:对于我来说,写比看容易多了……
我觉得与其光把代码写在这里,不如把大概思路简述下,然后再公布代码,那样说不定别人根据你的思路,会写出更漂亮、更适合自己使用的,呵呵
我觉得与其光把代码写在这里,不如把大概思路简述下,然后再公布代码,那样说不定别人根据你的思路,会写出更漂亮、更适合自己使用的,呵呵
作者: shanhun59 发布时间: 2008-10-22
SQL写在外面所能改的,经过此扩展类后,依然可以改

作者: kebiaowang 发布时间: 2008-10-22
回复3#
绝对是原创,你怎么说是光盘里的呢?
绝对是原创,你怎么说是光盘里的呢?
作者: tonlywang 发布时间: 2008-10-22
看一下,路过
作者: cnITonline.com 发布时间: 2008-10-25
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28