I have 2 tables, one looks like this:
TABLE ONE
id | Last Name | First Name | Username | Password | Secret Question
and another that looks like this:
TABLE TWO
id | Hobby | Country |
I want to combine a Select statement that grabs data from both tables and output the results. The following code:
$select = mysql_query("
SELECT * FROM table_one WHERE Username = 'Bob'
UNION ALL
SELECT * FROM table_two WHERE Hobby = 'Baseball'
");
while ($return = mysql_fetch_assoc($select)) {
$userName = $return['Username'];
$hobby = $return['Hobby'];
}
echo "$userName likes $hobby";
results in a The used SELECT statements have a different number of columns error, what am I doing wrong?
EDIT:
Running this gives the following error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/studentw/public_html/foo.php on line 16
$select = mysql_query("
SELECT FROM TABLE_ONE t1
INNER JOIN TABLE_TWO t2
ON t1.id = t2.id
WHERE t1.Username = 'Bob'
AND t2.Hobby = 'Baseball'
");
while ($return = mysql_fetch_assoc($select)) {
$firstName = $return['Username'];
$hobby = $return['Hobby'];
}
echo "$firstName likes $hobby";
The values in the tables are:
TABLE ONE
id | Last Name | First Name | Username | Password | Secret Question
1 | Hughes | Bobby | Bob | 123 | Maiden name?
TABLE TWO
id | Hobby | Country |
1 | Baseball | USA