smartguyin Posted December 26, 2006 Share Posted December 26, 2006 Can i fetch data from 2 or more tables in one single query...I have about 3 to 4 categories and the use having content in all the 3 to 4 catogires for which i created different different table...Please see my Code here[code]<p><center><h1>My Property Details</h1></center></p><br><?phpecho "<h3>FLATS ADDED BY YOU</h3>";$query = "SELECT * FROM fl WHERE fl_userid = '".$_SESSION['user_logged']."'"; $result = mysql_query($query) or die("Error: " . mysql_error());echo "<table border=\"1\">\n";while ($row = mysql_fetch_assoc($result)) {echo "<tr>\n";foreach($row as $value) {echo "<td>";echo $value;echo "</td>\n";}echo "</tr>\n";}echo "</table><br><hr>\n";////////////////////////////////////echo "<h3>LAND ADDED BY YOU</h3>";$query = "SELECT * FROM ld WHERE ld_userid = '".$_SESSION['user_logged']."'"; $result = mysql_query($query) or die("Error: " . mysql_error());echo "<table border=\"1\">\n";while ($row = mysql_fetch_assoc($result)) {echo "<tr>\n";foreach($row as $value) {echo "<td>";echo $value;echo "</td>\n";}echo "</tr>\n";}echo "</table><br><hr>\n";//////////////////////////////////////echo "<h3>RENT PROPERTIES ADDED BY YOU</h3>";$query = "SELECT * FROM re WHERE re_userid = '".$_SESSION['user_logged']."'"; $result = mysql_query($query) or die("Error: " . mysql_error());echo "<table border=\"1\">\n";while ($row = mysql_fetch_assoc($result)) {echo "<tr>\n";foreach($row as $value) {echo "<td>";echo $value;echo "</td>\n";}echo "</tr>\n";}echo "</table><br><hr>\n";?>/////////////////////////////////////////////////////////////////[/code]I this i have 3 categories -- Flat, Land and Rent... I want to display all the listing in all the tables i.e. of Flat Land and Rent..... is tht possible in single QueryPlease help me........ Link to comment https://forums.phpfreaks.com/topic/31874-query-help/ Share on other sites More sharing options...
ted_chou12 Posted December 26, 2006 Share Posted December 26, 2006 I dont quite understand, could you please explain more?ThanksTedEDITED: what i dont understand is the category 3 and category 4... Link to comment https://forums.phpfreaks.com/topic/31874-query-help/#findComment-147898 Share on other sites More sharing options...
chiprivers Posted December 26, 2006 Share Posted December 26, 2006 Do you have the same columns in each table? Link to comment https://forums.phpfreaks.com/topic/31874-query-help/#findComment-147900 Share on other sites More sharing options...
chiprivers Posted December 26, 2006 Share Posted December 26, 2006 If you have the same columns you should be able to use UNION quite easily, try:[code]$query = "SELECT * FROM fl WHERE fl_userid = '".$_SESSION['user_logged']."'UNIONSELECT * FROM ld WHERE ld_userid = '".$_SESSION['user_logged']."'UNIONSELECT * FROM re WHERE re_userid = '".$_SESSION['user_logged']."'";[/code] Link to comment https://forums.phpfreaks.com/topic/31874-query-help/#findComment-147901 Share on other sites More sharing options...
smartguyin Posted December 26, 2006 Author Share Posted December 26, 2006 No these categories are in different tables but when the user post in any of the category username is saved in the every table which he has posted....like there are 3 tables forFlat - flLand - ldRent - resooo i want to call in all the tables where username is "$username" Link to comment https://forums.phpfreaks.com/topic/31874-query-help/#findComment-147902 Share on other sites More sharing options...
ted_chou12 Posted December 26, 2006 Share Posted December 26, 2006 so there are three tables which are named fl, ld, re, respectively, and what you want is display the result side by side?but what are you colums? we need to know your columns, the name of the columns. Link to comment https://forums.phpfreaks.com/topic/31874-query-help/#findComment-147905 Share on other sites More sharing options...
smartguyin Posted December 26, 2006 Author Share Posted December 26, 2006 columns in every table are like .... id, area, price, username, address Link to comment https://forums.phpfreaks.com/topic/31874-query-help/#findComment-147907 Share on other sites More sharing options...
ted_chou12 Posted December 26, 2006 Share Posted December 26, 2006 then you want to display all columns as well? ie. all: id, area, price, username, address...?or you just want one, eg. only the area Link to comment https://forums.phpfreaks.com/topic/31874-query-help/#findComment-147908 Share on other sites More sharing options...
ted_chou12 Posted December 26, 2006 Share Posted December 26, 2006 [code]<?php$query = "SELECT fl.area, ld.area, re.area ". "FROM fl, ld, re ". "WHERE fl.username = ld.username = re.username"; $result = mysql_query($query) or die(mysql_error());while($row = mysql_fetch_array($result)){ echo $row['area']. " - ". $row['area']. " - ". $row['area'];}//this gets the value of "area" from all three tables for the username you give the script, but you have to have your username in all three tables, or else there would be an error. Tell me if this is what you want or not.?>[/code] Link to comment https://forums.phpfreaks.com/topic/31874-query-help/#findComment-147910 Share on other sites More sharing options...
smartguyin Posted December 26, 2006 Author Share Posted December 26, 2006 okay cool got it..... Thanks........ Link to comment https://forums.phpfreaks.com/topic/31874-query-help/#findComment-147911 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.