-2

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!

5
  • I do not see anything, that might be link this two tables together. Is there anything? Could you please describe structure of those tables? Commented Jul 30, 2013 at 5:00
  • 1
    how is the data in contacts,category related ? Commented Jul 30, 2013 at 5:00
  • How does the 2 tables relate? Does a contact have a category_id specified? Is there a joining table? Is the relationship 1<->1, 1-> many, many<->many? Commented Jul 30, 2013 at 5:01
  • Ultimately, I'm trying to pull and assign fields to variables, then write it so that I can add category values to the category row inside the contacts table. Maybe, I'm going about it the wrong way? Commented Jul 30, 2013 at 5:03
  • Any relation between both table ? Commented Jul 30, 2013 at 5:08

2 Answers 2

4

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 = (?)"
Sign up to request clarification or add additional context in comments.

1 Comment

Put comma in between A data and B data.This will return Cartesian result.
2

if your two tables have a comman attribute you can use inner join to do this

select * from tableA inner join tableB 
on tableA.commanKey =tableB.commanKey on tableA.id=?  

hope this will help you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.