WordPress企业主题定制/开发/优化

PHP学习资料之PHP数据库操作类

首页 » PHP » PHP学习资料之PHP数据库操作类

PHP学习资料之PHP数据库操作类

<?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
?>

分类与标签:

PHP

, ,

相关项目

  • WordPress外贸企业主题

  • 最近更新

  • 热门标签