tags:

views:

112

answers:

2

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
+3  A: 

I think you want a JOIN, not a UNION or UNION ALL.

SELECT FROM TABLE_ONE t1
  INNER JOIN TABLE_TWO t2
    ON t1.id = t2.id
WHERE t1.Username = 'Bob'
  AND t2.Hobby = 'Baseball'

Unions require that the data columns of each table be the same number and type, and basically give you a concatenation of multiple rows from different tables. Joins, on the other hand, essentially expand one table into a wider one with more columns by literally joining another table's columns to it.

When you do a join, you need to specify how the rows of one table correspond to the other; in this case, I'm assuming that your Id field is supposed to be a primary/foreign key linking the tables together. Apologies if that is incorrect-- if that is so, I will need more information in order to properly help you.


Per your edited question:

I'll first link you to the documentation for the mysql_query function. It has one required and one optional argument; the second argument is your connection handle. If you don't specify it, then PHP assumes that the last connection opened with mysql_connect is the one you want to use. So my first question is, did you call mysql_connect properly and did that call work successfully?

If you're sure the mysql_connect call worked, then I'm not sure what the problem could be. I don't think it could hurt, though, to assign the result of mysql_connect to a variable so you can explicitly specify the connection in mysql_query. Maybe something like the following:

$conn = mysql_connect("localhost:3306", "username", "password");
$query = "select * from some_table"; // obviously use your own query here
$result = mysql_query($query, $conn);

Let me know if that doesn't work.

Platinum Azure
Please see my edit, I tried running the code you posted but it gives another error. Sorry I am very new to mysql. As for id, yes the id columns on both tables are primary key and the related link between both tables.
Lisa
A side note, you don't want the primary keys to indicate the relationship between two tables. You should have a foreign key on one of the tables.
FlySwat
Yes to what Fly Swat says - if you have more than one hobby for a person, your design will fail.Also I hate the use of ID as a primary key. It is not helpful at all and it is not clear osuggest you get inthe habit of naming ids for the table they are primary key in and then using the same name for any foriegn ley tables. This wil make amintenance much simpler as the relationships are much more clear.
HLGEM
@HLGEM: In general I agree, however I was just trying to make the original author's code work with the design she (I presume) had.
Platinum Azure
A: 

Yes, Platinum Azure is correct, what you need is a join. However, your table design needs some help.

You need to relate the tables to each other. Easy way to do this is:

1) Add a column to table 1: "hobby_id" 2) Then each record record in table 1 needs to have the id from table 2 in the field "hobby_id"

Table 1:

id | Last Name | First Name | Username | Password | Secret Question | hobby_id
1 | Hughes | Bobby | Bob | 123 | Maiden name? | 2
2 | Smith | Mike | Msmith | mypass | usual | 1

Table 2:

id | Hobby | Country | 
1 | Baseball | USA
2 | Hockey | Canada
3 | Horse Racing | Japan

See how hobby_id relates to id in table 2? So, Table 1, Bobby, plays Hockey and Mike plays Baseball. No one matches horse racing.

So to query these tables, it would look like this:

select table1.`last name`,table1.`first name`, table2.`hobby`  from table1
inner join table2 on table1.hobby_id = table2.id

The result would be:

Hughes | Bobby | Hockey
Smith | Mike | Baseball

and your php code then looks like this:

while ($return = mysql_fetch_assoc($select)) {

    echo $return['first name']." ".$return['last name']." likes ".$return['hobby']."<br/>\n";

}
Lucas
-1: Having the foreign key be in the user table (referencing the hobby table) allows for a one-to-many relationship between users and hobbies. Your design limits this relationship to a one-to-one relationship. I dunno about you, but I don't just have StackOverflow as my only hobby. :-)
Platinum Azure
...Though I guess I'm a little off too. What should really happen is that a many-to-many join table should be used. There's no reason why a hobby should only belong to one user either. But I think you're still off about requiring a one-to-one relationship.
Platinum Azure