imarockstar Posted April 29, 2009 Share Posted April 29, 2009 I am trying to join some tables and had a small question ... Here is my code for joining tables : <?php // Retrieve data from database $sql="SELECT * FROM shows INNER JOIN bands ON shows.bandid = bands.id where shows.sid = '$sid' && shows.userid = ". $_SESSION['id']; $result=mysql_query($sql); // Start looping rows in mysql database. $rows = mysql_fetch_array($result); ?> year: <? echo $rows['year']; ?> <?php } ?> So what I have above works great. However if I want to display data from both tables, but the field name is the same in both, how could i do that .. example .. both tables have a filed names ID, which is an integer ... so how could I display the ID from each table ? would it be something like this, which i tried and did not work : id 1: <? echo $rows['table1.id']; ?> id 2: <? echo $rows['table2.id']; ?> Does that make since ? on another note ... anyone know where to find the leaked transformers 2 trailer .. I found a crappy version on a russion website .. but it was hard to watch .. lol Link to comment https://forums.phpfreaks.com/topic/156140-solved-mysql-query-and-display-question/ Share on other sites More sharing options...
Ken2k7 Posted April 29, 2009 Share Posted April 29, 2009 Give the table ID an alias. SELECT shows.id AS showid, bands.id AS bandid ... Link to comment https://forums.phpfreaks.com/topic/156140-solved-mysql-query-and-display-question/#findComment-821925 Share on other sites More sharing options...
imarockstar Posted April 29, 2009 Author Share Posted April 29, 2009 where would that go in the query, is there a certain order of how the query should be written ? Link to comment https://forums.phpfreaks.com/topic/156140-solved-mysql-query-and-display-question/#findComment-821931 Share on other sites More sharing options...
Ken2k7 Posted April 29, 2009 Share Posted April 29, 2009 where would that go in the query, is there a certain order of how the query should be written ? $sql="SELECT shows.id AS showid, bands.id AS bandid, * FROM shows INNER JOIN bands ON shows.bandid = bands.id where shows.sid = '$sid' && shows.userid = ". $_SESSION['id']; Like that. Use showid and bandid wherever you want to reference IDs. It's self-explanatory which id is which, right? Link to comment https://forums.phpfreaks.com/topic/156140-solved-mysql-query-and-display-question/#findComment-821933 Share on other sites More sharing options...
imarockstar Posted April 29, 2009 Author Share Posted April 29, 2009 ya man .. lol .. just was not sure if it went before or after the WHERE .. etc etc .. thanks for the help !!! Link to comment https://forums.phpfreaks.com/topic/156140-solved-mysql-query-and-display-question/#findComment-821944 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.