从数据库中选择字段中的id

时间:2016-04-14 12:04:17

标签: php mysql

我有一个新闻数据库我在每个新闻中使用多只猫,所以我在数据库中有一个名为cats的数据库,我在其中插入数据,就像1,5,8,2这个数据是猫的id。

我使用此代码从数据库中选择猫

$select_newscats = $mysqli->query("SELECT * FROM news_cats where show_home = '1' and  kind = 1 or kind = 2 order by ord_show asc");

while ($rows_newscats = $select_newscats->fetch_array(MYSQL_ASSOC)){

    $id_newscats          = $rows_newscats ['id'];
    $title_newscats       = $rows_newscats ['title'];
    $ord_show_newscats    = $rows_newscats ['ord_show'];
    $icon_newscats        = $rows_newscats ['icon'];
    $kind_newscats        = $rows_newscats ['kind'];
    $description_newscats = $rows_newscats ['description'];

}

我想从数据库中选择新闻,其中猫$id_newscats在新闻表中提交,因此我将此代码转移到新闻数据

$select_newscats = $mysqli->query("SELECT * FROM news_cats where show_home = '1' and  kind = 1 or kind = 2 order by ord_show asc");

while ($rows_newscats = $select_newscats->fetch_array(MYSQL_ASSOC)){

    $id_newscats          = $rows_newscats ['id'];
    $title_newscats       = $rows_newscats ['title'];
    $ord_show_newscats    = $rows_newscats ['ord_show'];
    $icon_newscats        = $rows_newscats ['icon'];
    $kind_newscats        = $rows_newscats ['kind'];
    $description_newscats = $rows_newscats ['description'];


    $select_news = $mysqli->query("SELECT * FROM news where $id_newscats  IN (cats)");
    while ($rows_news = $select_news->fetch_array(MYSQL_ASSOC)){

        $id_new          = $rows_news ['id'];
        $title_news       = $rows_news ['title'];

        echo "<div>{$title_news}</div>";
    }
}

但是这个代码有问题它只选择第一个例如,如果我有这个猫ID 5,2,7它只给我这个ID 5的新闻并忽略{{ 1}}所以我该如何解决这个问题,并选择所有新闻,其中这个标识2,7在猫领域谢谢。

4 个答案:

答案 0 :(得分:1)

看起来有语法(1)和逻辑(2)错误。

  1. SELECT * FROM news where $id_newscats IN (cats)错了。

    这是正确的语法。
    $select_news = $mysqli->query("SELECT * FROM news where cats IN ($id_newscats)")
  2. 在查询中使用paranthesis。

    SELECT * FROM news_cats where show_home = '1' and (kind = 1 or kind = order by ord_show asc"

答案 1 :(得分:0)

您的IN查询顺序错误。

$select_news = $mysqli->query("SELECT * FROM news where cats IN ($id_newscats)")

答案 2 :(得分:0)

您没有在外部while循环中构建IN列表!你得到一行,然后在内部循环中你只有一个id来搜索,所以不需要IN语法。 无论如何哪个错误

您的外部查询也看起来错了,我认为您需要围绕OR进行一些括号。

$select_newscats = $mysqli->query("SELECT * 
                                   FROM news_cats 
                                   WHERE show_home = '1' 
                                    and  ( kind = 1 or kind = 2 ) 
                                   ORDER BY ord_show asc");

while ($rows_newscats = $select_newscats->fetch_array(MYSQL_ASSOC)){

    $id_newscats          = $rows_newscats ['id'];
    $title_newscats       = $rows_newscats ['title'];
    $ord_show_newscats    = $rows_newscats ['ord_show'];
    $icon_newscats        = $rows_newscats ['icon'];
    $kind_newscats        = $rows_newscats ['kind'];
    $description_newscats = $rows_newscats ['description'];

    // amended query, you only have one $id_newscats
    //  each time round this outer loop
    $select_news = $mysqli->query("SELECT * 
                                   FROM news 
                                   WHERE id = $id_newscats");

    while ($rows_news = $select_news->fetch_array(MYSQL_ASSOC)){

        $id_new          = $rows_news ['id'];
        $title_news       = $rows_news ['title'];

        // no need for {} round scalar variables
        //echo "<div>{$title_news}</div>";
        echo "<div>$title_news</div>";

        // or you could just dothis and dont bother 
        // creating an unecessary scalar variable
        //echo "<div>{$rows_news['title']}</div>";
    }
}

答案 3 :(得分:0)

我得到了这个

更改此

$select_news = $mysqli->query("SELECT * FROM news where $id_newscats  IN (cats)");

进入这个

$select_news = $mysqli->query("SELECT * FROM news where FIND_IN_SET('$id_newscats', cats) ");