/**
* @author zWX212114
* @todo MYSQL select
* @param $sql SQL语句字符串
* @param $params 条件参数数组
* @return Array $result
* @example 此处只是示范,具体的参数设置请自行设置
* $params = array(
* 'where' => array(
* 'and' => array(
* 'id = 1',
* 'title LINKE %标题%',
* ),
* 'or' => array(
* 'id = 1',
* 'title LINKE %标题%',
* ),
* ),
'order' => 'id DESC',
'limit' => ($nowpagenum-1)*$pagesize.','.$pagesize,
);
$list = $this->getArticalModel() ->fetchAll($params);
* @license 此处仅仅是日常使用如有特殊需要请自行处理
*/
public function dbSelect($sql=null,$params=null){
$this->dbLink();
$num = 0;
//检测是否有SQL语句
if(empty($sql)){
$sql = 'SELECT * FROM '.$this->table;
}else{
$sql = $sql;
}
//检测是否设置有其他附加选项where-and、or,order,limit
if(!empty($params)){
if(isset($params['where']) && !empty($params['where'])){
if(is_array($params['where'])){
if(isset($params['where']['and']) && !empty($params['where']['and'])){
$whereand = join(' AND ', $params['where']['and']);
}else{
$whereand = null;
}
if(isset($params['where']['or']) && !empty($params['where']['or'])){
$whereor = join(' OR ', $params['where']['or']);
}else{
$whereor = null;
}
if($whereand == null && $whereor == null){
$where = ' ';
}else{
if($whereand == null){
$where = ' WHERE '. $whereor;
}else if($whereor == null){
$where = ' WHERE '.$whereand;
}else{
$where = ' WHERE '.$whereand .' OR '. $whereor;
}
}
}else{
$where = ' WHERE '. $params['where'];
}
}else{
$where = ' ';
}
if(isset($params['order']) && !empty($params['order'])){
$order = ' ORDER BY '.$params['order'];
}else{
$order = ' ';
}
if(isset($params['limit']) && !empty($params['limit'])){
$limit = ' LIMIT '.$params['limit'] . ' ; ';
}else{
$limit = ' ';
}
$sqlString = $sql.$where.$order.$limit;
}else{
$sqlString = $sql;
}
//调试输出SQL语句
//echo $sqlString;
$query = mysql_query($sqlString);
$result = array();
while($row = mysql_fetch_array($query)){
$result[$num] = $row;
$num++;
}
mysql_free_result($query);
return $result;
}
MYSQL select 封装
最新推荐文章于 2022-07-19 20:39:28 发布