Jump to content

Query help !!!


smartguyin

Recommended Posts

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>

<?php
echo "<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 Query

Please help me........
Link to comment
https://forums.phpfreaks.com/topic/31874-query-help/
Share on other sites

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']."'
UNION
SELECT * FROM ld WHERE ld_userid = '".$_SESSION['user_logged']."'
UNION
SELECT * 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

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 for

Flat  -  fl
Land - ld
Rent - re



sooo 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

[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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.