数据库操作类

<?php
 class Model {
  private $result; //执行query命令的结果资源标识
private $sql; //sql执行语句
private $row; //返回的条目数
private $coding; //数据库编码,GBK,UTF8,gb2312
private $bulletin = true; //是否开启错误记录
private $show_error = true; //测试阶段,显示所有错误,具有安全隐患,默认关闭
private $is_error = false; //发现错误是否立即终止,默认true,建议不启用,因为当有问题时用户什么也看不到是很苦恼的
  private $dbLink=NULL;//数据库连接句柄
  public function __construct(){
  global $C;
  $this->dbLink=mysql_connect($C['DB_HOST'], $C['DB_USER'], $C['DB_PWD']) or exit(mysql_error());
  mysql_select_db($C['DB_NAME'], $this->dbLink) or exit(mysql_error());
mysql_query("SET NAMES {$C['DB_CHAR']}");
  }
   //查询
  public function Query($sql){
  if ($sql == "") {
$this->show_error("SQL语句错误:", "SQL查询语句为空");
}
$this->sql = $sql;
$result = mysql_query($sql);
if (!$result) {
//调试中使用,sql语句出错时会自动打印出来
if ($this->show_error) {
$this->show_error("错误SQL语句:", $this->sql);
}
} else {
$this->result = $result;
}
return $this->result;
  }
    public function Fetch(){
      return @mysql_fetch_array($this->result);
    }
    //全部查询FindAll
    public function FindAll($table){
      $this->Query("SELECT * FROM $table");
    }
    //简化查询
    public function Select($table,$columnName="*",$condition='',$debug=''){
      $condition = $condition?'WHERE'.$condition:NULL;
      if($debug){
      echo "SELCET $columnName FROM $table $condition";
      }else{
      $this->Query("SELECT $columnName FROM $table $condition");
      }
    }
     //简化删除del
    public function Delete($table,$condition,$url=''){
if($this->Query("DELETE FROM $table WHERE $condition")){
if (!empty ($url))
                $this->Get_admin_msg($url, '删除成功!');
}
    }
    //简化插入insert
public function Insert($table, $array) {
$field="";
$value="";
if(!is_array($array) || count($array)<=0){
$this->Get_admin_msg("数字为空或者不是数组!");
exit();
}
while(list($key,$val)=each($array)){
$field .="$key,";
$value .="'$val',";
}
$field=substr($field,0,-1);
$value=substr($value,0,-1);
$sql=$this->Query("INSERT INTO $table($field) values($value)");
}

//简化修改update
public function Update($table, $array, $condition, $url = '') {
                $field="";
                $value="";
                if(!is_array($array) || count($array)<=0){
                        $this->Get_admin_msg($url,"没有要更新的内容!");
                        exit();
                }
                while(list($key,$val)=each($array)){
                        $value .="$key='$val'";
                }
                $value=substr($value,0,-1);
                $this->Query("INSERT INTO $table SET $value where $condition");             
}
    //取得上一步产生ID
    public function Insert_Id(){
      return mysql_insert_id();
    }
    // 根据select查询结果计算结果集条数
    public function Nums(){
      if($this->result == NULL){
      if ($this->show_error) {
$this->show_error("SQL语句错误", "暂时为空,没有任何内容!");
}
      }else{
      return mysql_num_rows($this->result);
      }
    }
    //释放结果集
     public function Free(){
      @mysql_free_result($this->result);
     }
//析构函数,自动关闭数据库,垃圾回收机制
public function __destruct(){
if(!empty($this->result)){
$this->Free();
}
mysql_close($this->dbLink);
}
//输出显示sql语句
public function show_error($message = "", $sql = "") {
if (!$sql) {
echo "<font color='red'>" . $message . "</font>";
echo "<br />";
} else {
echo "<fieldset>";
echo "<legend>错误信息提示:</legend><br />";
echo "<div style='font-size:14px; clear:both; font-family:Verdana, Arial, Helvetica, sans-serif;'>";
echo "<div style='height:20px; background:#000000; border:1px #000000 solid'>";
echo "<font color='white'>错误号:12142</font>";
echo "</div><br />";
echo "错误原因:" . mysql_error() . "<br /><br />";
echo "<div style='height:20px; background:#FF0000; border:1px #FF0000 solid'>";
echo "<font color='white'>" . $message . "</font>";
echo "</div>";
echo "<font color='red'><pre>" . $sql . "</pre></font>";
$ip = $this->getip();
if ($this->bulletin) {
$time = date("Y-m-d H:i:s");
$message = $message . "\r\n$this->sql" . "\r\n客户IP:$ip" . "\r\n时间 :$time" . "\r\n\r\n";

$server_date = date("Y-m-d");
$filename = $server_date . ".txt";
$file_path = "error/" . $filename;
$error_content = $message;
//$error_content="错误的数据库,不可以链接";
$file = "error"; //设置文件保存目录

//建立文件夹
if (!file_exists($file)) {
if (!mkdir($file, 0777)) {
//默认的 mode 是 0777,意味着最大可能的访问权
die("upload files directory does not exist and creation failed");
}
}

//建立txt日期文件
if (!file_exists($file_path)) {

//echo "建立日期文件";
fopen($file_path, "w+");

//首先要确定文件存在并且可写
if (is_writable($file_path)) {
//使用添加模式打开$filename,文件指针将会在文件的开头
if (!$handle = fopen($file_path, 'a')) {
echo "不能打开文件 $filename";
exit;
}

//将$somecontent写入到我们打开的文件中。
if (!fwrite($handle, $error_content)) {
echo "不能写入到文件 $filename";
exit;
}

//echo "文件 $filename 写入成功";

echo "——错误记录被保存!";

//关闭文件
fclose($handle);
} else {
echo "文件 $filename 不可写";
}

} else {
//首先要确定文件存在并且可写
if (is_writable($file_path)) {
//使用添加模式打开$filename,文件指针将会在文件的开头
if (!$handle = fopen($file_path, 'a')) {
echo "不能打开文件 $filename";
exit;
}

//将$somecontent写入到我们打开的文件中。
if (!fwrite($handle, $error_content)) {
echo "不能写入到文件 $filename";
exit;
}

//echo "文件 $filename 写入成功";
echo "——错误记录被保存!";

//关闭文件
fclose($handle);
} else {
echo "文件 $filename 不可写";
}
}

}
echo "<br />";
if ($this->is_error) {
exit;
}
}
echo "</div>";
echo "</fieldset>";

echo "<br />";
}
/*获得客户端真实的IP地址*/
function getip() {
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
$ip = getenv("HTTP_CLIENT_IP");
} else
if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
$ip = getenv("HTTP_X_FORWARDED_FOR");
} else
if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
$ip = getenv("REMOTE_ADDR");
} else
if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
$ip = $_SERVER['REMOTE_ADDR'];
} else {
$ip = "unknown";
}
return ($ip);
}
public function Get_admin_msg($url, $show = '操作成功!') {
$msg = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link rel="stylesheet" href="css/common.css" type="text/css" />
<meta http-equiv="refresh" content="2; URL=' . $url . '" />
<title>管理区域</title>
</head>
<body>
<div id="man_zone">
  <table width="30%" border="1" align="center"  cellpadding="3" cellspacing="0" class="table" style="margin-top:100px;">
    <tr>
      <th align="center" style="background:#cef">信息提示</th>
    </tr>
    <tr>
      <td><p>' . $show . '<br />
      2秒后返回指定页面!<br />
      如果浏览器无法跳转,<a href="' . $url . '">请点击此处</a>。</p></td>
    </tr>
  </table>
</div>
</body>
</html>';
echo $msg;
exit ();
}
 }
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值