Trouble fetching joined field

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xlomo
Forum Newbie
Posts: 2
Joined: Sat Sep 20, 2008 9:30 pm

Trouble fetching joined field

Post by xlomo »

I use the following query at a web app

$res=mysql_query("SELECT posts.postID , posts.title, members.username, posts.postdate, posts.userID FROM members INNER JOIN posts ON members.id = posts.userID WHERE posts.userID =".$_GET['userpost']);

This query runs fine when i run it at phpmyadmin but, when i run it from my page i can't fetch the members.username field.

I use this code to show the rows

for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
Print "<tr>";
Print "<td><a href='readpost.php?postID=".$row[postID]."' class='style1'>".$row[title]."</a></td>";
Print "<td>".$row[username]."</td>";
Print "<td>".$row[postdate]."</td>";
//Print "<td>".$row[catname]."</td>";
Print "</tr>";
}

I also tried to change the query to "SELECT posts.postID AS Pid, posts.title AS Pttl, members.username AS USRname ...., and to access the fields by using int ($row[0]...)
but none of them worked.
So, can someone tell me what i do wrong ?
xlomo
Forum Newbie
Posts: 2
Joined: Sat Sep 20, 2008 9:30 pm

Re: Trouble fetching joined field

Post by xlomo »

OK i found it, between

$res=mysql_query("SELECT posts.postID , posts.title, members.username, posts.postdate, posts.userID FROM members INNER JOIN posts ON members.id = posts.userID WHERE posts.userID =".$_GET['userpost']);

and

for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
Print "<tr>";
Print "<td><a href='readpost.php?postID=".$row[postID]."' class='style1'>".$row[title]."</a></td>";
Print "<td>".$row[username]."</td>";
Print "<td>".$row[postdate]."</td>";
//Print "<td>".$row[catname]."</td>";
Print "</tr>";
}

intervenes html code and some php includes (ex <?php include 'login.php'; ?>) in which php I use $res for other queries and so it's overwritten.

Fewwwwwwwwwwwww :crazy:
Post Reply