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 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理