Results from the most recent live auction are here .
24 members in the live chat room. Join Chat !
04-09-2008, 05:26 AM
· #1 The original NP Emo Kid
Name: Liam Dawe
Location: Plymouth, UK
Join Date: Jan 2005
Posts: 1,562
NP$: 91.00 (
Donate )
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?
04-09-2008, 01:45 PM
· #2 NamePros Member
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
04-09-2008, 05:59 PM
· #3 while ($awake){ code(); }
Name: Eric
Location: Kentucky
Join Date: Mar 2005
Posts: 4,196
NP$: 1662.00 (
Donate )
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
04-10-2008, 04:30 AM
· #4 The original NP Emo Kid
Name: Liam Dawe
Location: Plymouth, UK
Join Date: Jan 2005
Posts: 1,562
NP$: 91.00 (
Donate )
Thanks for the replies and yes it is not needed to give each field a name unless they have the same name.
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off