discuz x2.5中 fetch_all函数求解释
public static function fetch_all($sql, $arg = array(), $keyfield = '', $silent=false) {
$data = array();
$query = self::query($sql, $arg, $silent, false);
while ($row = self::$db->fetch_array($query)) {
if ($keyfield && isset($row[$keyfield])) {
$data[$row[$keyfield]] = $row;
} else {
$data[] = $row;
}
}
self::$db->free_result($query);
return $data;
}
请大侠解释下这个函数,最好每句代码与函数参数代表的意思都解释下。谢谢啊!
------解决思路----------------------
<br />/*<br />* $sql 要执行的sql语句<br />* $arg sql中要用到的参数(数组)<br />* $keyfield 返回数组中的键名(若有)<br />* $silent 已以哪种方式查询(默认是mysql_query,还有mysql_unbuffered_query)<br />*/<br />public static function fetch_all($sql, $arg = array(), $keyfield = '', $silent=false) {<br /> <br /> $data = array();<br /> $query = self::query($sql, $arg, $silent, false);//执行查询<br /> while ($row = self::$db->fetch_array($query)) {<br /> //将查询的资源放到$data数组中,若$keyfield有值,则将$keyfield作为$data的key值<br /> if ($keyfield && isset($row[$keyfield])) {<br /> $data[$row[$keyfield]] = $row;<br /> } else {<br /> $data[] = $row;<br /> }<br /> }<br /> self::$db->free_result($query);//释放资源<br /> return $data;//二维数组<br />}<br />
<br />//查询uid为1的用户的所有信息<br />$res=DB::fetch_all("SELECT * FROM %t WHERE uid=%d",array('common_member',1));<br />