喜欢面向对象的进来,看看我这个类,怎样写会更好,更容易让别人看懂,也更容易用于开发

看小雪 2014-03-01 06:31:38
喜欢面向对象的进来,看看我这个类,怎样写会更好,更容易让别人看懂,也更容易用于开发。此类为数据库操作的类,只用于PDO且数据库为sqlite。当然,高手也可以改进下。

前景:以前我怕sqlite数据库出错,所以用了两个数据库,因此类里面有传数据库的地址。也算是我前期设计的一个失误吧。当然,能设计成静态调用更好。
<?php
/*
此版本为utf-8(无BOM)版本,因为GBK编码在SQLITE3下处理不当会出现乱码。
延续旧版本部分功能
*/

class WEB {
private $conn ;
private $stmt ;
public function __construct() {
$this->CheckURL();
}
public function __destruct(){
$this->conn=null;
$this->stmt=null;
}
public function CheckURL() {
$BadUrl = "'|and|create|delete|select|update|union|;|*|(|)";
$url = empty($_SERVER["QUERY_STRING"])?"":$_SERVER["QUERY_STRING"];
if(!empty($url)) {
$arr = explode($BadUrl,"|");
foreach($arr as $v) {
if(stripos($url,$v)>0) {
$this->CatchError("URL含有敏感字符");
exit;
}
}
}
}

public function CatchError($str='') {
echo '<fieldset style="width:350px;padding: 3px;"><legend> 错误描述 </legend><br> 捕捉到错误,程序结束。 <p><font style="color:#ff0000;font-size:12px;">',$str, '</font></p><a href="http://'.$_SERVER['SERVER_NAME'].'" target="_blank" title="迷茫时代技术支持" style="margin: 3px 0px 2px 2px;color:#000;text-decoration: underline;">迷茫时代</a></fieldset>';
exit();
}
//数据库操作区-------------------------------------
public function ConnectDB($str) {
if(!isset($str) || empty($str)) $this->CatchError("连接数据库字符串不能为空");
if(!is_file($str)) $this->CatchError("没有此文件$str");
$this->conn = new PDO('sqlite:'.$str) or $this->CatchError('连接数据库错误');

return $this->conn;
}


public function lastInsertId(){
return $this->conn->lastInsertId();
}

public function query($str1,$db,$str2='',$str3='') {
if(empty($str1)) $this->CatchError('查询数据库错误');
if(!empty($str2)) {
$str2 = $this->SQLParam($str2);
$str1 = $str1.$str2;
}
if(!empty($str3)) $str1 = $str1.$str2.$str3;
if(!$this->conn) $this->conn = $this->ConnectDB($db);

$err = $this->conn->errorInfo();
$result = $this->conn->query($str1) or $this->CatchError("查询出错PDO<br>".$err[2]);
$result->setFetchMode(PDO::FETCH_ASSOC);
return $result;
}
public function querySingle($str1,$db,$str2=1) {
if(empty($str1) or empty($db)) $this->CatchError('SQL is empty!');
if(!$this->conn) $this->conn = $this->ConnectDB($db);
$result = $this->conn->query($str1);
if($str2==1) {
$row = $result->fetch(PDO::FETCH_ASSOC);
return $row;
}else{
$row = $result->fetch(PDO::FETCH_NUM);
return $row[0];
}
}
public function exec($str1,$db) {
if(empty($str1)) $this->CatchError('SQL is empty!');
if(!$this->conn) $this->conn = $this->ConnectDB($db);
return $this->conn->exec($str1);
}
public function prepare($sql,$db) {
if(empty($sql)) $this->CatchError('prepare need SQL!');
if(!$this->conn) $this->conn = $this->ConnectDB($db);
$this->stmt = $this->conn->prepare($sql);
}
public function bindValue($name,$value,$type) {
if($type == 'TEXT') {
$this->stmt->bindValue($name,$value,PDO::PARAM_STR);
}else if($type == 'INT') {
$this->stmt->bindValue($name,$value,PDO::PARAM_INT);
}
}
public function execute() {
$this->stmt->execute();
$this->stmt->closeCursor();
}
public function errorInfo() {
$arr = $this->stmt->errorInfo();
$this->CatchError($arr[2]);
$arr = NULL;
}
public function fetchColumn($int) {
if(!is_numeric($int)) $this->CatchError('fetchColumn must be a number');
$int = abs(intval($int));
return $this->stmt->fetchColumn($int) or $this->CatchError('the number is not in fetchColumn!');
}
public function SQLParam($str) {
if(is_null($str) || empty($str) || (!isset($str))) return '';
$str = str_replace("'","",$str);
$str = str_replace("\\","",$str);
$str = str_replace("\"","",$str);
$str = str_replace("&","",$str);
$str = str_replace("#","",$str);
$str = str_replace(";","",$str);
$str = str_replace("%","",$str);
$str = str_replace("`","",$str);
$str = preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml);/", "$1", $str);
$str = preg_replace("/&(.)(uml);/", "$1e", $str);

return $str;
}
//数据库操作区结束--------------------------------------------------

function version(){
return array(
//'server' => $this->conn->getAttribute(PDO::ATTR_SERVER_INFO),
'client version' => $this->conn->getAttribute(PDO::ATTR_CLIENT_VERSION),
'driver name' => $this->conn->getAttribute(PDO::ATTR_DRIVER_NAME),
'server version' => $this->conn->getAttribute(PDO::ATTR_SERVER_VERSION),
//'connection' => $this->conn->getAttribute(PDO::ATTR_CONNECTION_STATUS)
);
}


}
?>
...全文
50 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧