two tables in one query

Discussion in 'MySQL' started by adamlacombe, Apr 4, 2011.

  1. #1
    I am trying to select data from two tables at once. I have no clue what I am doing as you will see.. This is what I have for code:
    
    <?php
    include ('includes/db.php');
    include ('includes/functions.php');
    include ('includes/global.php');
    session_start();
    
    
    echo '<table width="100%">';
    $statsql="SELECT feed.id AS fid, wall.id AS wid, feed.type AS ftype, feed.by AS fby, feed.pageid AS fpageid from `feed`, `wall` ORDER BY `wall`.`id` DESC LIMIT 0,10";
    $statres=mysql_query($statsql) or die(mysql_error());
    
    	while($statrow=mysql_fetch_array($statres)){
    		$message=clean_up($statrow[message]);
    		$date=clean_up($statrow[date]);
    		$time=clean_up($statrow[time]);
    		$by=clean_up($statrow[by]);
    
    
    $usersql="SELECT * from `users` WHERE `id`='$by'";
    $useres=mysql_query($usersql) or die(mysql_error());
    
    	while($row=mysql_fetch_array($useres)){
    		$avatar=clean_up($row[avatar]);
    		$first=clean_up($row[first]);
    
    echo "<tr class='content'><td valign='top'>
    <img src='pic.php?w=50&amp;h=50&amp;constrain=1&amp;img=photos/$avatar' /><br>$first"; } echo "</td><td valign='top'>
    $message</td></tr>";
    }
    
    
    $fid=clean_up($statrow[fid]);
    $pageid=clean_up($statrow[fpageid]);
    $type=clean_up($statrow[ftype]);
    $by=clean_up($statrow[fby]);
    include("../includes/activity_info.php");
    
    
    echo "</table>";
    ?>
    
    Code (markup):
    the include for activity info is a file that echos out data from the table "feed".

    Right now I am just getting a blank page. Can you see what needs to be fixed?

    Thanks in advanced!
     
    adamlacombe, Apr 4, 2011 IP
  2. adamlacombe

    adamlacombe Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I been messing around with the code a little bit. I got it to display data from both tables. Only problem is, table "wall" only has one row of data and its showing it about 10 times. Then the second tables data is displaying but I want both tables data displayed as one table. I want it so things are ordered by time/date.

    this is the code that I have now after messing around with it

    
    <?php
    include ('includes/db.php');
    include ('includes/functions.php');
    include ('includes/global.php');
    session_start();
    
    
    echo '<table width="100%">';
    $statsql="SELECT wall.by AS wby, wall.date AS wdate, wall.time AS wtime, wall.message AS wmessage, feed.pageid, feed.by, feed.type FROM `wall`, `feed` LIMIT 0,10";
    $statres=mysql_query($statsql) or die(mysql_error());
    
    	while($statrow=mysql_fetch_assoc($statres)){
    		$message=clean_up($statrow[wmessage]);
    		$date=clean_up($statrow[wdate]);
    		$time=clean_up($statrow[wtime]);
    		$by=clean_up($statrow[wby]);
    
    
    $usersql="SELECT * from `users` WHERE `id`='$by'";
    $useres=mysql_query($usersql) or die(mysql_error());
    
    	while($row=mysql_fetch_assoc($useres)){
    		$avatar=clean_up($row[avatar]);
    		$first=clean_up($row[first]);
    
    echo "<tr class='content'><td valign='top'>
    <img src='pic.php?w=50&amp;h=50&amp;constrain=1&amp;img=photos/$avatar' /><br>$first"; } echo "</td><td valign='top'>
    $message</td></tr>";
    
    
    $pageid=clean_up($statrow[pageid]);
    $type=clean_up($statrow[type]);
    $by=clean_up($statrow[by]);
    include("../includes/activity_info.php");
    }
    
    echo "</table>";
    ?>
    
    Code (markup):
     
    adamlacombe, Apr 4, 2011 IP
  3. codeartist

    codeartist Peon

    Messages:
    96
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you must join the two table using join or where condition

    Look at your wall and feed table script

    SELECT wall.by AS wby, wall.date AS wdate, wall.time AS wtime, wall.message AS wmessage, feed.pageid, feed.by, feed.type FROM `wall`, `feed` LIMIT 0,10

    it has joined no where possible solution

    SELECT wall.by AS wby, wall.date AS wdate, wall.time AS wtime, wall.message AS wmessage, feed.pageid, feed.by, feed.type FROM `wall`, `feed` join wall.id=fee.wallid LIMIT 0,10
     
    codeartist, Apr 5, 2011 IP