Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-16-2011, 05:41 PM   PM User | #1
coding_begins
Regular Coder

 
Join Date: Aug 2011
Posts: 134
Thanks: 20
Thanked 0 Times in 0 Posts
coding_begins is an unknown quantity at this point
join two tables

I need help in joining 2 tables and display data:
Right now i am just tryin to display by using multiple while loops.
First i am obtaining the pmtextid where the messageread field is 0 from pm table.
Then I am fetching details from pmtext table.
Again I need to fetch the pmid from pm table depending on the details fetched from the pmtext table.
Can i do all these through one join statement:


Code:
<?
$getpmtext=mysql_query("SELECT pmtextid from pm WHERE messageread='0' ORDER by pmid desc") or die (mysql_error());
                while($getpmtext_results=mysql_fetch_assoc($getpmtext))
                {
                   //$pmid=$getpmtext_results['pmid'];
                   $pmtextid=$getpmtext_results['pmtextid'];

                   $getnewpm=mysql_query("SELECT * from pmtext WHERE pmtextid='$pmtextid' AND dateline >='$datelast' AND dateline <='$datenow'") or die (mysql_error());
                   while($getnewpm_results=mysql_fetch_assoc($getnewpm))
                   {
                      $newpmtextid=$getnewpm_results['pmtextid'];
                      $pmtitle=$getnewpm_results['title'];
                      $fromusername=$getnewpm_results['fromusername'];
                      $fromuserid=$getnewpm_results['fromuserid'];

                      $getnewpmid=mysql_query("SELECT pmid from pm WHERE pmtextid='$newpmtextid'") or die (mysql_error());
                      while($getnewpmid_results=mysql_fetch_assoc($getnewpmid))
                      {
                         $newpmid=$getnewpmid_results['pmid'];
                         echo "NEW PM:<b> &nbsp;".$pmtitle."</b>&nbsp;FROM:&nbsp;<b>".$fromusername."</b><br>";
                      }
                    }
                 }
?>
coding_begins is offline   Reply With Quote
Old 09-16-2011, 08:45 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,188
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Yes, and you should.
Code:
SELECT t.title, t.fromusername, pm2.pmid AS newpmid 
FROM pm AS pm1, pmtext, pm AS pm2
WHERE messageread=0
AND pmtext.pmtextid = pm1.pmtextid
AND dateline BETWEEN '$datelast' AND '$datenow'
ORDER by pm1.pmid desc
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 09-16-2011, 08:46 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,188
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Why do you need the "newpmid" when you don't use it in the echo???
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Tags
join

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:40 PM.


Advertisement
Log in to turn off these ads.