mysql model层_No-PDO-Models-MySQL数据库层抽象类 - 实现

/**

*class Mysql_db

*

* mysql 数据库类,实现Database_interface 接口

*

* @param Database

* @author 王扶林

* @copyright 王扶林 2014-9-29

*

* @version 1.0

*

*/

require_once "Database_interface.php";

/**

*

*/

class Mysql_db implements Database_Interface

{

/**

* 数据库连接标识

* @var resource

* @access protected

*/

protected $_dbConnect = null;

/**

* 数据库查询结果集

* @var resource

* @access protected

*/

protected $_result = null;

/**

* 插入语句最后执行的最后自增索引值

* @var integer

* @access public

*/

public $insertId = 1;

/**

* isAssocArray()

* @param array $arr 待判断的数组

* @return boolean 若数组为关联数组,返回TRUE,否则返回FALSE

*/

static public function isAssocArray($arr)

{

return array_keys($arr) !== range(0,count($arr)-1);

}

/**

* __construct

* @param string $dbname 传入的数据库的名称(可选参数,默认为usr数据库)

* @param string $dbhost 传入的数据库的服务器地址(可选参数,默认为127.0.0.1)

* @param string $dbuser 传入的数据库账户名(可选参数,默认为root)

* @param string $dbpwd 传入的数据库账户名所对应的账户密码(可选参数,默认为***)

*/

public function __construct($dbname = 'usr',$dbhost = '127.0.0.1',

$dbuser = 'root',$dbpwd = '***')

{

$this->_dbConnect = @mysql_connect($dbhost, $dduser, $dbpwd);

//连接数据库

if (false == $this->_dbConnect) {

throw new Exception("数据库连接错误".mysql_error());

}

//选择数据库

if (!@mysql_select_db($dbname,$this->_dbConnect)) {

throw new Exception("数据库选择错误" . mysql_error());

}

//设置字符编码

$this->setCharset();

}

/**

* __destruct

*

*析构函数,释放结果集资源

*@return nulll

*

*/

public function __destruct(){

//释放结果集

$this->freeResult();

//关闭数据库连接

if ($this->_dbConnect) {

mysql_close($this->_dbConnect);

}

}

/**

* select

*

* 获得数据表中所有满足特定条件的记录

*

* @param string $tableName 必要参数,待查询的数据表

* @param array $condition 查询条件(可选参数,为一关联数组,默认为null )

* @param int $recordBegin 从某项记录开始查询(可选参数,默认情况为0,从第一条记录开始查询)

* @param int $recordLength 待查询记录的个数(可选参数,默认情况为所有记录)

* @param string $sortCol 待排序的字段名(可选参数,默认情况为不排序)

* @param boolean $desc 是否为降序排序(可选参数,默认为升序排序)

* @return array 有结果集组成的二维数组(每个元素为关联数组,代表一条记录)

*/

public function select($tableName, Array $condition = null,

$recordBegin = 0,$recordLength = 0,$sortCol = null,

$desc = false)

{

//构造SQL 语句

$sql = "SELECT * FROM {$tableName}";

//传入的条件参数为真并且是关联数组

if ($condition) {

if (!self::isAssocArray($condition)) {

//若不是关联数组

throw new Exception('必须传入一个关联数组的条件');

}else{

//遍历条件数组,构造条件语句

$sql .= $this->generateWhere($condition);

}

}

//处理是否排序

if ($sortCol) {

$sql .= " ORDER BY {$sortCol}";

}

//处理是否降序

if ($desc) {

$sql .= " DESC";

}

//处理记录的个数 (若处理的记录个数不为 0)

if (0 != $recordLength) {

$sql .= " limit {$recordBegin} ,{$recordLength}";

}else if(0 != $recordBegin){

$sql .= " limit {$recordBegin}";

}

//执行SQL 语句

$this->_result = @mysql_query($sql);

if (!$this->_result) {

throw new Exception("执行SQL语句出错". mysql_error());

}

//获得查询结果

$rows = array();

while ($row = mysql_fetch_assoc($this->_result)) {

$rows[] = $row;

}

//释放结果集

$this->freeResult();

//返回结果集组成的关联数组

return $rows;

}

public function delete($tableName ,Array $condition)

{

//构造SQL语句

$query = "DELETE FROM {$tableName} ";

//由条件数组,构造删除条件

if (!self::isAssocArray($condition)) {

//若不是关联数组

throw new Exception("必须传入一个关联数组的条件");

}else{

$query .= $this->generateWhere($condition);

}

//执行SQL语句

$result = @mysql_query($query);

if (!$this->_result) {

throw new Exception("执行SQL语句出错".mysql_error());

}

//返回受影响个数

return mysql_affected_rows();

}

/**

* insert()

* @param string $tableName 待插入的数据表名

* @param Array $records 带插入的记录所组成的二维数组

* @return int 所受影响的行数

*/

public function insert($tableName, Array $records)

{

//构造SQL语句

$query = "INSERT INTO {$tableName} VALUES";

//待处理插入的记录数组

$query .= ' (';

foreach($records as $red){

//处理每一条插入记录

//生成记录信息

foreach($red as $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, '()')), ',');

//echo $query;

//echo "
";

//执行SQL 语句

$this->_result = @mysql_query($query);

if(!$this->_result){

throw new Exception("插入SQL 语句出错". mysql_error());

}

//获得插入记录的最大自增索引

$this->insertId = @mysql_insert_id();

//返回受影响的个数

return mysql_affected_rows();

}

public function update($tableName,Array $condition, Array $newRecord)

{

//构造SQL语句

$query = "UPDATE {$tableName} SET ";

//

if (!self :: isAssocArray($newRecord)) {

throw new Exception('记录的新值必须传入一个关联数组的条件');

} else {

//生成记录信息

foreach ($newRecord as $key => $value) {

$nKey = " {$key} = ";

if (is_string($value) &&

($value !== 'null') && ($value !== 'now()')) { //若$value为字符串

$nKey .= "'{$value}',";

} else {

$nKey .= "{$value},";

}

}

$nKey = rtrim($nKey, ',');

$query .= $nKey;

}

//由传入的条件数组,构造更新条件

if (!self :: isAssocArray($condition)){ //若不为关联数组

throw new Exception('必须传入一个关联数组的条件');

} else {

$query .= $this->generWhere($condition);

}

//执行SQL语句

$result = @mysql_query($query);

if (!$this->_result) {

throw new Exception('执行SQL语句出错.' . mysql_error());

}

//返回受影响个数

return mysql_affected_rows();

}

/**

* selectAll()

*

* 获得数据表中的所有记录的所有字段

*

* @param string $tableName 待查询的数据表名

* @return array 所有记录组成的一个二维数组(每个元素为一个关联数组,代表一条记录)

*/

public function selectAll($tableName)

{

return $this->select($tableName);

}

/**

* selectById

*

* 通过主键获得某一条记录,主键由可选参数传入

*

* @param string $tableName 数据表名

* @param integer $id 获得记录的主键ID值 (可选参数默认值ID为1)

* @param string $key 主键的字段名(可选参数,默认为ID)

* @return array 所获得记录产生的关联数组

*/

public function selectById($tableName, $id = 1,$key = 'id')

{

//构造query语句

$query = "SELECT * FROM {$tableName} WHERE {$key} = {$id}";

//执行SQL 语句

$this->_result = @mysql_query($query);

if (!$this->_result) {

throw new Exception("执行主键查询出错".mysql_error());

}

//获得查询结果

if (1 != @mysql_num_rows($this->_result)) {

throw new Exception("主键记录查询出现多条" . mysql_error());

}

$rows = mysql_fetch_assoc($this->_result);

//释放结果集

$this->freeResult();

//返回结果集组成的关联数组

return $rows;

}

/**

* selectBySql()

*

* 通过传入的SQL语句,获得查询结果

*

* @param string $query 待查询的SQL语句

* @return array $rows 所有记录组成的一个二维数组(每个元素为关联数组,代表一条记录)

*/

public function selectBySql($query)

{

//执行SQL语句

$this->_result = @mysql_query($query);

if (!$this->_result) {

throw new Exception('执行SQL语句出错.' . mysql_error());

}

//获得查询结果

$rows = array();

while ($row = mysql_fetch_assoc($this->_result)) {

$rows[] = $row;

}

//释放结果集

$this->freeResult();

//返回结果集组成的关联数组

return $rows;

}

/**

* setCharset

*

* 设置Mysql数据库显示字符编码,由传入参数决定字符编码

*

* @param string $charset 待设置的字符编码(可选参数,默认为UTF8)

* @return null 无

*/

private function setCharset($charset = 'UTF-8')

{

if ($this->_dbConnect) {

mysql_query("set names {$charset}",$this->_dbConnect);

}

}

/**

* generateWhere()

*由传入的条件数组,构造where子句

*

* @param Array $condition 由条件语句组成的关联数组的

* @return string 构造生成的Where语句字符串

*/

private function generateWhere(Array $condition)

{

$con = "WHERE";

foreach($condition as $key => $value){

if (is_string($value)) {

$con .= "{$key} = '{$value}' and";

}else{

$con .= "{$key} = {$value}";

}

}

$con = rtrim($con ,'and');

return $con;

}

/**

* freeResult

*

* 释放MySQL结果集资源

*

* @param null 无

* @return null 无

*/

public function freeResult()

{

if ($this->_result) {

if (!@mysql_free_result($this->_result)) {

throw new Exception("释放结果集出错" . mysql_error());

}

$this->_result = null;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值