选择一些包含特定INT的SQL行

时间:2015-10-23 07:47:14

标签: php mysql sql

我的数据库存在问题。我不知道如何找到包含特定INT的SQL行。我有一个新闻列表,如果这个消息是发布的,则INT是1.这是我的代码:

$base = $db->query('SELECT id, title, image, content, date, slug, publish FROM articles ORDER BY id DESC');

while($row = $base->fetch()) {

    if ($row['publish'] == 1) {

        /* NEWS */

    }

}

提前感谢您的回复。

2 个答案:

答案 0 :(得分:2)

在MySQL查询中添加where publish = 1: -

$base = $db->query('SELECT id, title, image, content, date, slug, 
publish FROM articles where publish=1 ORDER BY id DESC');

不需要这种情况: -

if ($row['publish'] == 1)

答案 1 :(得分:1)

无需办理登机手续while(),仅在select中添加条件。

$base = $db->query('SELECT id, title, image, content, date, slug, publish 
FROM articles WHERE publish = 1 ORDER BY id DESC');
              ^                ^