Home> php教程> php手册> body text

Model修改之添加getOne函数

WBOY
Release: 2016-06-07 11:41:39
Original
1811 people have browsed it

Model修改之添加getOne函数:只查询一条数据
Model中有find获取一条数据,但是只能用于本身的模型
Model中有query函数根据sql查询一个列表,但是对于只查询一条数据来说,有些不方便,所以重新写了一个只查询一条数据库的函数:/**
* SQL查询(只取一条数据)
* @access public
* @param string $sql SQL指令
* @param mixed $parse 是否需要解析SQL
* @return mixed
*/
public function getOne($sql,$parse=false) {
if(!is_bool($parse) && !is_array($parse)) {
$parse = func_get_args();
array_shift($parse);
}
if (!preg_match("/LIMIT/i", $sql)) {
$sql = preg_replace('/[,;]$/i', '', trim($sql)) . ' LIMIT 1;';
}
$sql = $this->parseSql($sql,$parse);
$resultSet = $this->db->query($sql);
if(false === $resultSet) {
return false;
}
if(empty($resultSet)) {
return null;
}
return $resultSet[0];
}

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!