duanhang032796
2013-07-30 04:58
浏览 97
已采纳

SELECT * FROM有两个不同的表[重复]

This question already has an answer here:

I've been fiddling with this for bit and tried a couple different ways to combine both SELECT FROM statements, but I can't get it to work where I can call on fields from two tables within the same database. It works find if I remove either one. I tried to combine them using UNION (not sure if that's the correct thing), but couldn't get it to work.

Here's what I got so far with them separated:

$id = $_POST['id'];

$edit = $db->prepare("SELECT * FROM contacts WHERE id = (?)");
$edit->bind_param('s', $id);
$edit->execute();
$edit->bind_result($id, $firstName, $lastName, $email, $phone, $category);

$categories = $db->prepare("SELECT * FROM category");
$categories->execute();
$categories->bind_result($cateid, $setcategory);

echo $firstName;

while ($categories->fetch()) {
    echo $setcategory;
}

I'm new to all of this. I'd be grateful if someone could point me in the right direction. Thanks!

</div>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doumei4964 2013-07-30 05:08
    已采纳

    if the two tables have a common field then you can do something like

    "SELECT A.*, B.*
     FROM contacts AS A
     JOIN category AS B
     ON A.primary = B.foreign
     WHERE A.id = (?)"
    
    评论
    解决 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题