Isabella
Isabella

Reputation: 65

How to multiple tables?

I would like to retrieve data (the name in this case) from two tables and display them into a table. The columns are the same in both of the tables.

Can I do something like this:

$result=mysql_query("select table1.name, table2.name 
                      from table1, table2 where   id='$pid'");

Upvotes: 0

Views: 85

Answers (2)

Wes Crow
Wes Crow

Reputation: 2967

Try this:

$result = mysql_query("select table1.name as 'table1_name', table2.name as 'table2_name' from table1, table2 where table1.id='$pid' and table2.id='$pid'");

Also, note that if the value of $pid is coming from an outside source, you should really prepare the data before you query it against the server. You can create your own filter function or you can utilize the mysqli::prepare function that does the work for you through data binding.

Upvotes: 1

rodrigoap
rodrigoap

Reputation: 7480

change column's name like this:

$result=mysql_query("select t1.name name1, t2.name name2 
                      from table1 t1, table2 t2 
                      where t1.id=t2.id and t1.id='$pid'");

Upvotes: 1

Related Questions