[advanced search]
Results from the most recent live auction are here.
24 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Go Back   NamePros.Com > Design and Development > Programming
User Name
Password

Old 04-09-2008, 05:26 AM   · #1
liam_d
The original NP Emo Kid
 
liam_d's Avatar
 
Name: Liam Dawe
Location: Plymouth, UK
Trader Rating: (28)
Join Date: Jan 2005
Posts: 1,562
NP$: 91.00 (Donate)
liam_d is a name known to allliam_d is a name known to allliam_d is a name known to allliam_d is a name known to allliam_d is a name known to allliam_d is a name known to all
join query, how to output?

Hi all, i have this query i need to echo out all the contents in a loop:

Code:
SELECT n.poster_id, n.date, n.subject, n.post, m.mid, m.username FROM news n INNER JOIN members m ON n.poster_id = m.mid ORDER BY n.nid DESC LIMIT 6


Although my while loop using a mysql_fetch_array gives this error:

Quote:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/altercod/public_html/mecha00.com/index2modules/news.php on line 19



What is the correct way to loop through the data of a joined sql statement? And then echo it out?


Please register or log-in into NamePros to hide ads
liam_d is offline   Reply With Quote
Old 04-09-2008, 01:45 PM   · #2
Skeeter
NamePros Member
 
Skeeter's Avatar
 
Trader Rating: (0)
Join Date: Mar 2008
Posts: 31
NP$: 0.00 (Donate)
Skeeter is an unknown quantity at this point
Arrow aliases

I see you resolved this but just in case someone else wants to know...

When you use a join query in PHP you have to alias the column names

example:

SELECT
n.poster_id AS Poster_ID,
n.date AS Date_Whatever,
n.subject AS Subject,
n.post AS Post,

You can then reference your columns in your PHP code by using the aliases

PHP Code:
$str_PosterID=$row_Recordset1['Poster_ID'];


If you resolved the issue some other way please share. Thanks
Skeeter is offline   Reply With Quote
Old 04-09-2008, 05:59 PM   · #3
SecondVersion
while ($awake){ code(); }
 
SecondVersion's Avatar
 
Name: Eric
Location: Kentucky
Trader Rating: (140)
Join Date: Mar 2005
Posts: 4,196
NP$: 1662.00 (Donate)
SecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond repute
Member of the Month
MOTM September 2005 Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Cancer Alzheimer's Protect Our Planet
Originally Posted by Skeeter
I see you resolved this but just in case someone else wants to know...

When you use a join query in PHP you have to alias the column names

example:

SELECT
n.poster_id AS Poster_ID,
n.date AS Date_Whatever,
n.subject AS Subject,
n.post AS Post,

You can then reference your columns in your PHP code by using the aliases

PHP Code:
$str_PosterID=$row_Recordset1['Poster_ID'];


If you resolved the issue some other way please share. Thanks


Not necessarily. You'd only really need to alias if you're selecting/using columns from both tables, that are the same. Eg: if you need to use 'username', but you're SELECT'ing the 'username' field from both tables.

This could have been resolved with:
Code:
SELECT n.poster_id, n.date, n.subject, n.post, m.mid, m.username FROM news n, members m WHERE n.poster_id = m.mid ORDER BY n.nid DESC LIMIT 6


OR

Code:
SELECT n.poster_id, n.date, n.subject, n.post, m.mid, m.username FROM news AS n LEFT JOIN members AS m ON (n.poster_id = m.mid) ORDER BY n.nid DESC LIMIT 6
__________________

SecondVersion.com - The Personal Blog of SecondVersion
Domain Name Portfolio - Get your free copy. - Version 1.0.1 now available!!
MetaCreator.com - Free Meta Tag Creator
CodingPlanet.com - Coming soon...
SecondVersion is offline   Reply With Quote
Old 04-10-2008, 04:30 AM   · #4
liam_d
The original NP Emo Kid
 
liam_d's Avatar
 
Name: Liam Dawe
Location: Plymouth, UK
Trader Rating: (28)
Join Date: Jan 2005
Posts: 1,562
NP$: 91.00 (Donate)
liam_d is a name known to allliam_d is a name known to allliam_d is a name known to allliam_d is a name known to allliam_d is a name known to allliam_d is a name known to all
Thanks for the replies and yes it is not needed to give each field a name unless they have the same name.
liam_d is offline   Reply With Quote
Reply

NamePros is a revenue sharing forum.

Thread Tools

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Site Sponsors
Get Your Site Linked at LinkedKeywords.com http://www.mobisitetrader.com/ http://www.mobisitetrader.com/
Advertise your business at NamePros
All times are GMT -7. The time now is 04:01 PM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0