Page 1 of 1

run 2 mysql_query's

Posted: Wed Feb 06, 2008 4:02 am
by dj scousey
Hi all, i have just created a shoutbox using php & sql.

the output is put into tables by the echo feature.

what i would like to do is, place the users Email address (which is stored in a database) as the td title="" . The thing is though, that the data which i need to "grab" from the table is in a different table than the "shouts" (its in the login table). So i was wondering if i could have some code like the following:

Code: Select all

function getShouts()
{   
    
 
    $query = mysql_query("SELECT * FROM shouts ORDER BY id DESC LIMIT 25") or die(mysql_error());
    $query1 = mysql_query("SELECT * FROM members") or die(mysql_error());
        
    while ($row = mysql_fetch_array($query, $query1) )
    {
 
        
        $name = stripslashes($row['Shoutname']);
        $contact = stripslashes($row['Contact']);
        $shout = stripslashes($row['Shout']);       
        $email = stripslashes($row['Email']);
        
        
        if(empty($contact))
        {
        
            echo '<tr><td title="'.$email.'" class="phpshout_posts"><p><span class="author"><a href="mailto:'.$contact.'">'.$name.'</a></span> - <span 
 
class="shout">'.$shout.'<hr width=105 color=#ffffFF></span></p></td></tr>';
            
        } else {
        
 
            echo '<tr><td title="'.$domain.'" class="phpshout_posts"><p><span class="author"><a href="mailto:'.$contact.'" >'.$name.'</a></span> - <span 
 
class="shout">'.$shout.'<hr width=105 color=#ffffFF></span></p></td></tr>';
 
        } 
                
    }
any help please??

Re: run 2 mysql_query's

Posted: Fri Feb 08, 2008 12:24 am
by GuitarheadCA
The solution you are probably looking for is to use a JOIN in your SQL, and merge the two queries. Assuming you use a member id column to link the two tables, it might look something like this:

Code: Select all

 
SELECT s.shoutname, s.shout, 
            m.contact, m.email
FROM shouts s
INNER JOIN members m
ON s.memberid = m.memberid
 
That should get you all the data in one query that use can use to loop through.