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

Model修改之添加getOne函数

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

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

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template