【发布时间】:2011-07-09 09:27:30
【问题描述】:
我正在尝试在课堂上构建自己的 CMS。
现在,当我尝试从 MySQL 数据库中获取数据时遇到了问题
而不是一个项目,我想得到我所有项目的集合
最后我想得到一个对象,这样我就可以像这样读出它:$item->id
这是我的代码:
static function getContentItems($id, $active, $sort_by, $sort_type, $limit) {
if (isset($id) && !empty($id)) {
$where .= "WHERE id = ".$id;
}
if (isset($active) && !empty($active)) {
$where .= " AND active = ".$active;
}
if (isset($sort_by) && !empty($sort_by)) {
$where .= " ORDER BY ".$sort_by;
if (isset($sort_type) && !empty($sort_type)) {
$where .= " ".$sort_type;
}
}
if (isset($limit) && !empty($limit)) {
$where .= " LIMIT 0,".$limit;
}
if (isset($where) && !empty($where)) {
$query = "SELECT * FROM content ".$where;
} else {
$query = "SELECT * FROM content";
}
$result = mysql_query($query)or die(mysql_error());
$item = new ContentItem();
while ($data = mysql_fetch_array($result)) {
$item->id = $data['id'];
}
return $item;
}
}
【问题讨论】:
-
不用写
isset($where) -
如果你想将它添加到
$item,最好使用mysql_fetch_object
标签: php mysql class collections