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

看小雪 看小雪 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
喜欢面向对象的进来,看看我这个类,怎样写会更好,更容易让别人看懂,也更容易用于开发。此类为数据库操作的类,只用于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)
);
}


}
?>
...全文

相关推荐

这是一个写日志类,可用于实际项目开发应用。

这是一个写日志类,可用于实际项目开发应用。实现原理是创建一个队列,然后将日子字符串不断加入队列,启动一个线程将队列中的字符串写入日志文件,可以设置报错级别

zjszjs0056

自己写的一个AS雷电游戏的类

AS写的雷电的一个类,看了他你们基本都能看懂,有不懂的可以留言或者喜欢这个游戏代码的可以联系我316031501

zwlljl

unity-entity-component-system:一种更好的游戏设计方法,使您可以专注于要解决的实际问题:构成游戏的数据和行为。 通过从面向对象的设计过渡到面向数据的设计,您将更容易重用代码,而其他人也更容易理解和使用它

实体组件系统 一种更好的游戏设计方法,可让您专注于要解决的实际问题:构成游戏的数据和行为。 通过从面向对象的设计过渡到面向数据的设计,您将更容易重用代码,而其他人也更容易理解和使用它。 ≪··≫ 嗨! 我叫Jeffrey Lanters,感谢您检查我的模块! 多年来,我一直是Unity开发人员,直到2020年,我决定通过开放源代码开始共享我的模块。 所以随便看看。 如果您将此模块用于生产,请考虑捐赠以支持该项目。 使用任何软件包时,请确保在此存储库中加注星标,并感谢您在应用或游戏中某处的Jeffrey Lanters 。 另外请记住,禁止在诸如Unity Asset Store之类的商店

婉君 喜欢DIY

java-面向对象的英雄类

这是关于java的面向对象测试题,要是这个测试题能够自己独立做出来,说明你java入门了,希望大家继续努力。

dayaoK

coretran:一个易于遵循的库,通过包装的接口,排序例程,kD树和其他用于处理科学数据和概念的算法,通常可使Fortran变得更容易。 该库包含核心的fortran例程和面向对象的类

Coretran 一个易于遵循的库,通过包装的接口,排序例程,kD树和其他用于处理科学数据和概念的算法,通常可使Fortran变得更容易。 该库包含核心的fortran例程和面向对象的类。 为什么选择coretran? 在博士期间,我广泛使用Fortran来解决超大型方程组,并将其应用于3D地下物理属性模型的地球物理数据反演。 我开发了利用空间中的随机点云,结构化直线形,三角形和voronoi网格以及这些结构的非结构化版本的算法。 与其他语言相比,Fortran具有相对较少的现代且免费的源代码,而其他语言则具有易于使用的库来执行这些类型的操作。 作为初学者,我很难进入Fortran,因为处理数字的基本功能尚不可用。 令人沮丧的是,我在分配内存或打开文件时不得不编写(然后重复)自己的错误检查。 当我不得不多次为不同的输入类型(例如整数或实数)编写相同的函数/子例程时,我也感到沮丧。

Tristan Du
评论
收藏 已收藏
分享
返回顶部
看小雪
基础编程 21,889 成员 140,329 内容