当前位置: 首页 > 后端技术 > PHP

No-PDO-Models-MySQL数据库层抽象类-实现

时间:2023-03-29 20:29:01 PHP

数据库抽象层实现mysql_connect(deprecated)_dbConnect=@mysql_connect($dbhost,$dduser,$dbpwd);//连接数据库if(false==$this->_dbConnect){thrownewException("数据库连接错误".mysql_error());}//选择数据库if(!@mysql_select_db($dbname,$this->_dbConnect)){thrownewException("数据库选择错误".mysql_error());}//设置字符编码$this->setCharset();}/***__destruct**析构函数,释放结果集资源*@returnnulll**/publicfunction__destruct(){//释放结果集$this->freeResult();//关闭数据库连接if($this->_dbConnect){mysql_close($this->_dbConnect);}}/***select**获取数据表中满足一定条件的所有记录**@paramstring$tableName必选参数,要查询的数据表*@paramarray$condition查询条件(可选参数,关联数组,默认为null)*@paramint$recordBegin从某条记录开始查询(可选参数,默认0,从第一条记录开始查询)*@paramint$recordLength要查询的记录条数(可选参数),默认为所有记录)*@paramstring$sortCol待排序的字段名(可选参数,默认不排序)*@paramboolean$desc是否按降序排序(可选参数,默认为升序)*@returnarray由结果集组成的二维数组(每个元素是一个关联数组,代表一条记录)*/publicfunctionselect($tableName,Array$condition=null,$recordBegin=0,$recordLength=0,$sortCol=null,$desc=false){//构造SQL语句$sql="SELECT*FROM{$tableName}";//传入的条件参数为真,是关联数组if($condition){if(!self::isAssocArray($condition)){//如果不是关联数组thrownewException('必须传入关联数组的条件');}别的{//遍历条件数组,构造条件语句$sql.=$this->generateWhere($condition);}}//处理是否排序if($sortCol){$sql.="ORDERBY{$sortCol}";}//是否降序处理if($desc){$sql.="DESC";}//处理的记录数(如果处理的记录数不为0)if(0!=$recordLength){$sql.="限制{$recordBegin},{$recordLength}";}elseif(0!=$recordBegin){$sql.="limit{$recordBegin}";}//执行SQL语句$this->_result=@mysql_query($sql);if(!$this->_result){thrownewException("执行SQL语句时出错".mysql_error());}//获取查询结果$rows=array();while($row=mysql_fetch_assoc($this->_result)){$rows[]=$row;}//释放结果集$this->freeResult();//返回结果集组成的关联数组return$rows;}publicfunctiondelete($tableName,Array$condition){//构造SQL语句$query="DELETEFROM{$tableName}";//从条件数组构造删除条件if(!self::isAssocArray($condition)){//如果不是关联数组thrownewException("必须传入关联数组的一个Condition");}else{$query.=$this->generateWhere($condition);}//执行SQL语句$result=@mysql_query($query);if(!$this->_result){thrownewException("执行SQL语句时出错".mysql_error());}//返回受影响的行数returnmysql_affected_rows();}/***insert()*@paramstring$tableName要插入的数据表名*@paramArray$records插入记录的二维数组*@returnint受影响的行数*/publicfunctioninsert($tableName,Array$records){//构造SQL语句$query="INSERTINTO{$tableName}VALUES";//待插入的记录数组$query.='(';foreach($recordsas$red){//处理每条插入的记录//生成记录信息foreach($redas$value){if(is_string($value)&&$value!=='null'&&$value!=='now()'){$query.="'{$value}',";}else{$query.="{$value},";}}//去掉value的最后一个字符连接,$query=rtrim($query,',');//关闭数据项的SQL语句$query.='),(';}$query.=')';$query=rtrim(rtrim(rtrim($query,'()')),',');//回显$查询;//回显“
”;//执行SQL语句$this->_result=@mysql_query($query);if(!$this->_result){thrownewException("插入SQL语句时出错".mysql_error());}//获取最大自加索引$this->insertId=@mysql_insert_id();//返回受影响的行数returnmysql_affected_rows();}publicfunctionupdate($tableName,Array$condition,Array$newRecord){//构造SQL语句$query="UPDATE{$tableName}SET";//if(!self::isAssocArray($newRecord)){thrownewException('记录的新值必须传入关联数组条件');}else{//生成记录信息foreach($newRecordas$key=>$value){$nKey="{$key}=";if(is_string($value)&&($value!=='null')&&($value!=='now()')){//如果$value是一个字符串$nKey.="'{$value}',";}else{$nKey.="{$value},";}}$nKey=rtrim($nKey,',');$查询.=$nKey;}//从传入的条件数组构造更新条件if(!self::isAssocArray($condition)){//如果不是关联数组thrownewException('必须传入一个关联数组条件');}else{$query.=$this->generWhere($condition);}//执行SQL语句$result=@mysql_query($query);if(!$this->_result){thrownewException('执行SQL语句时出错。'.mysql_error());}//返回受影响的个数returnmysql_affected_rows();}/***selectAll()**获取数据表中所有记录的所有字段**@paramstring$tableName要查询的数据表的名称*@returnarray所有记录的二维数组(每元素是一个关联数组,代表一条记录)*/publicfunctionselectAll($tableName){return$this->select($tableName);}/***selectById**通过主键获取记录,主键通过可选参数传入**@paramstring$tableName数据表名*@paraminteger$id获取主键ID值record(可选参数ID默认值为1)*@paramstring$key主键字段名(可选参数,默认ID)*@returnarray获取记录生成的关联数组*/publicfunctionselectById($tableName,$id=1,$key='id'){//构造查询语句$query="SELECT*FROM{$tableName}WHERE{$key}={$id}";//执行SQL语句$this->_result=@mysql_query($query);if(!$this->_result){thrownewException("执行主键查询时出错".mysql_error());}//获取查询结果if(1!=@mysql_num_rows($this->_result)){thrownewException("有多个主键记录查询".mysql_error());}$rows=mysql_fetch_assoc($this->_result);//释放结果Set$this->freeResult();//返回结果集组成的关联数组return$rows;}/***selectBySql()**通过传入的SQL语句获取查询结果**@paramstring$query待查询SQL语句*@returnarray$rows由所有记录组成的二维数组(每个元素为一个关联数组,表示一条记录)*/publicfunctionselectBySql($query){//执行SQL语句$this->_result=@mysql_query($query);if(!$this->_result){thrownewException('执行SQL语句时出错。'.mysql_error());}//获取查询结果$rows=array();while($row=mysql_fetch_assoc($this->_result)){$rows[]=$row;}//释放结果集$this->freeResult();//返回结果集组成的关联数组return$rows;}/***setCharset**设置Mysql数据库显示字符编码,字符编码由传入参数决定**@paramstring$charset要设置的字符编码(可选参数,默认为UTF8)*@returnnullNone*/privatefunctionsetCharset($charset='UTF-8'){if($this->_dbConnect){mysql_query("设置名称{$charset}",$this->_dbConnect);}}/***generateWhere()*从传入的条件数组构造where子句**@paramArray$condition由条件语句决定*@returnstringofthecomposedassociativearray构造生成的Where语句字符串*/private函数generateWhere(Array$condition){$con="WHERE";foreach($conditionas$key=>$value){if(is_string($value)){$con.="{$key}='{$value}'和";}else{$con.="{$key}={$value}";}}$con=rtrim($con,'and');返回$con;}/***freeResult**释放MySQL结果集资源**@paramnullNone*@returnnullNone*/publicfunctionfreeResult(){if($this->_result){if(!@mysql_free_result($this->_result)){thrownewException("释放结果集时出错".mysql_error());}$this->_result=null;}}}