Jump to content

[SOLVED] table joining help ....


imarockstar

Recommended Posts

I have 2 tables that I need to grab data from and show in a single output ...

 

I will try and explain this as best I can ...

 

 

Table 1 "users"

id,userid,username,email

(there are more rows then this, but these are all I need)

 

Table 2 "post"

id, userid, post

 

Basically I need to pull ALL the posts in DB,and then also list the USERID of each post.

 

The output looks like this:

http://franklinspirko.com/sites/isyatg/

 

or if you dont want to click the above link:

 

(post)

I was on the treadmill and I saw you running on the treadmill ahead of me !!!! I kept watching your ipod strapped to your shoulder. It kept going on and off ..

 

(user info)

mygym: La Fitness mylocation: 75023 postedby: benchgod @ 5:55pm

did you see me ?

 

post : the post the user posts (from table 2)

did you see me: this will be where I need to insert the USERID from table 1

 

Im not sure how to do this ... below is my current code Just pulling data from 1 table. How do I add another one and join them based on the USERID ?

 

<?php



// Retrieve data from database 
$sql="SELECT * FROM posts";
$result=mysql_query($sql);

// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
?>


<div class=postblock>


<div class=avatar>
<img class=avatar src="images/avatar.jpg" alt="image" width="90" >
</div> <!-- #avatar -->			

<div class=post>
<? echo $rows['post']; ?> 

<Br>

<div class=crumb><span class=crumb>mygym:</span> <span class=crumbdata>La Fitness</span></div>			

<div class=crumb><span class=crumb>mylocation:</span> <span class=crumbdata>75023</span></div>	

<div class=crumb>
<span class=crumb>postedby:</span> 
<span class=crumbdata>benchgod</span>
<span class=crumb>@</span> 
<span class=crumbdata>5:55pm</span>
</div>	

<br class=clear>		

<div class=seeme>did you see me ?</div>			

</div> <!-- #post -->

<br class=clear>

</div>	<!-- #postblock -->		




<? } mysql_close(); ?>

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

OK. But maybe you'd like to read it at source?

 

I assume you have no problems with SELECT * thing

 

Next thing:

FROM posts INNER JOIN users ON userid

translates to something like this:

"take records from posts, and try to find matching records from users, using uderid field as a match"

Link to comment
Share on other sites

Try this:

 

SELECT * FROM posts INNER JOIN users ON userid

 

Either

SELECT * FROM posts INNER JOIN users ON posts.userid = users.userid

 

or

 

SELECT * FROM posts INNER JOIN users USING (userid)

 

and avoid SELECT * unless you really do want all the column from both tables. In this case you only want post and user

Link to comment
Share on other sites

Actually from what I've read in documentation, the optimiser will resolve both to:

SELECT * FROM posts, users WHERE posts.userid = users.userID

which will give same results of course.

 

And yes. Choosing only needed columns will make things easier and faster.

Link to comment
Share on other sites

 

OK it worked without the session data .... here is my code ..

 

 

// Retrieve data from database

$result = mysql_query("SELECT * FROM users INNER JOIN usersinfo USING '{$_SESSION['userid']}' ") or die(mysql_error());

 

 

here is the error

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''2142747'' at line 1

 

Link to comment
Share on other sites

thats "where" lol .. i get confused ..

 

if I dont have userid in both tables .... how will it know that the userid is attached to a post ?

 

oh wait ...

 

in the other table ... the post table ... or whatever table i am using ... should i change "userid" to somthing else .. like ... postid .... but the data will be the same as user id .. so userid and post id will have the same "number" .. so when is searched for matches .. looks for the data and not the row name ??

 

am i even close ?

 

b

Link to comment
Share on other sites

You said you do have userid in both tables....

 

Table 1 "users"

id,userid,username,email

(there are more rows then this, but these are all I need)

 

Table 2 "post"

id, userid, post

 

Forget it... was reading to fast.

 

 

[edit]

 

You can explicitly state which 'userid' to use this way

WHERE users.userid = '{$_SESSION['userid']}'

Link to comment
Share on other sites

 

omg this is giving me a headache .. i remember why inthe past I stayed away from joining tables ...

 

 

table 1 - users  (holds the original userid and username and pass) ---  uses userid to hold the member #

table 2 - usersinfo (hold personal info of the users) --- uses infoid to hold the member #

 

 

code:

 

 

$result = mysql_query("SELECT * FROM users INNER JOIN usersinfo USING (userid) WHERE usersinfo.infoid = '{$_SESSION['userid']}' ") or die(mysql_error());

 

 

 

error

 

Unknown column 'franklin_gym.usersinfo.userid' in 'on clause'

 

Link to comment
Share on other sites

 

omg this is giving me a headache .. i remember why inthe past I stayed away from joining tables ...

 

 

table 1 - users  (holds the original userid and username and pass) ---  uses userid to hold the member #

table 2 - usersinfo (hold personal info of the users) --- uses infoid to hold the member #

 

 

code:

 

 

$result = mysql_query("SELECT * FROM users INNER JOIN usersinfo USING (userid) WHERE usersinfo.infoid = '{$_SESSION['userid']}' ") or die(mysql_error());

 

 

 

error

 

Unknown column 'franklin_gym.usersinfo.userid' in 'on clause'

 

 

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.