dran0703 2013-07-24 16:34
浏览 25
已采纳

通过ID使用单个查询从MySQL获取2个项目

I have a simple script, but it has just got a little more complex, at least for me.

so here it is, I need to request 2 menu buttons from table which contains all of them together. ID for both is known as it is already rendered by another script on the same page and wrapped in variables $cat_main_id and $cat_add_id. So here is my script, well at least a top part of it and my question is how I can get both menu buttons to be obtained by the same query, if that is actually possible?

    if ($cat_main_id > 0) {
    $menu_query = 'SELECT * FROM lcategories WHERE ID in ( :id , :id2 )';
    $res = $db->prepare($menu_query);
    $res->execute(array(':id' => $cat_main_id, ':id2' => $cat_add_id));
    foreach ($res as $info);
} else {
    echo '';
}
while ($info = $res -> fetch()){
    $category_name = str_replace("&", "&", $info['category_name']);
    $category_page = $info['category_page'];
    $category_dir = $info['category_folder'];
    $category_link = DS.$category_dir.DS.$category_page;
print<<<END

<div class="post_cat">Posted in category <a href="$category_link" class="cat">$category_name</a> <a href="$category_link" class="cat">$category_name</a></div>
<div class="clear"></div>

END;
}
  • 写回答

4条回答 默认 最新

  • dongningce9075 2013-07-24 16:40
    关注

    Yes easily, the key will be when you then get the results back to identify which row is which button

    if($cat_main_id > 0 && $cat_add_id > 0){
      $menu_query = 'SELECT * FROM lcategories WHERE ID in ( :id , :id2 )';
      $res = $db->prepare($menu_query);
      $res->execute(array(':id' => $cat_main_id, ,':id2' => $cat_add_id));
    }else{
      die(header("Location: error.php"));
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看