Getting Data from 3 MySQL tables

Ask about a PHP problem here.
Post Reply
Z645
Posts: 33
Joined: Thu Jul 26, 2012 5:08 pm

Getting Data from 3 MySQL tables

Post by Z645 »

Hi, I've been struggling with getting data from 3 tables (users, feeds and friends). I want my code to get the users friends feeds and display it on the homepage. (Only the users and no one elses, and users who are not friends cannot see it. Sort of like what Twitter and Facebook are doing.)

My tables:

users
[syntax=sql]CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
`first_name` varchar(32) NOT NULL,
`last_name` varchar(32) NOT NULL,
`email` varchar(1024) NOT NULL,
`active` int(11) NOT NULL DEFAULT '0',
`email_code` varchar(32) NOT NULL,
`avatar` varchar(55) NOT NULL,
`type` int(1) NOT NULL DEFAULT '0',
`protected` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;[/syntax]

feeds
[syntax=sql]CREATE TABLE IF NOT EXISTS `feeds` (
`id` int(6) NOT NULL AUTO_INCREMENT,
`user_id` int(6) NOT NULL,
`username` varchar(32) NOT NULL,
`body` text NOT NULL,
`date` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;[/syntax]

and buddys
[syntax=sql]CREATE TABLE IF NOT EXISTS `buddys` (
`id` int(6) NOT NULL AUTO_INCREMENT,
`user_one` int(6) NOT NULL,
`user_two` int(6) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;[/syntax]

What I'm using to fetch the users data and feeds etc
[syntax=php]$my_id = $_SESSION['user_id'];
if ($getquery = mysql_query("SELECT * FROM feeds f
INNER JOIN users u ON u.user_id = f.user_id
WHERE u.user_id = '$my_id' OR u.user_id IN
(SELECT b.user_one OR b.user_two FROM buddys b
WHERE b.user_one OR b.user_two = '$my_id')")){
// resultset processing goes here
while ($rows = mysql_fetch_assoc($getquery)){
$id = $rows['id'];
$user_id = $rows['user_id'];
$username = $rows['username'];
$body = $rows['body'];
$date = $rows['date'];
$dellink = "| <a href=\"http://localhost/vibeate/wigets/deletepost.php?id=" . $id . "\" style='color: grey;text-decoration:none;'>Delete</a>";
?>
<h2><a href="users/<?php echo $username; ?>"><?php echo $username; ?></a></h2>
<p><?php echo $body; ?></p><br />
Posted on <?php echo $date; ?> <?php if ($user_id == $user_data['user_id'] || is_admin($user_data['user_id']) === true){
echo $dellink;
}
?>
<br /><hr width="500px">
<?php
}
} else {
echo mysql_error();
}
if (empty($id)){
echo '<h3>There\'s no posts to show...Be the first to post</h3>';
}[/syntax]
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Getting Data from 3 MySQL tables

Post by ExtremeGaming »

Is there any reason why you can't just do like:

[syntax=php]<?php
session_start();
if($_SESSION['user_id'] == $_GET['user_id']) {
// Display
} else {
// Don't display
}
?>[/syntax]
<?php while(!$succeed = try()); ?>
Z645
Posts: 33
Joined: Thu Jul 26, 2012 5:08 pm

Re: Getting Data from 3 MySQL tables

Post by Z645 »

ExtremeGaming wrote:Is there any reason why you can't just do like:

[syntax=php]<?php
session_start();
if($_SESSION['user_id'] == $_GET['user_id']) {
// Display
} else {
// Don't display
}
?>[/syntax]

I don't think it'll work with what I'm doing. Unless you can explain further o-o
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Getting Data from 3 MySQL tables

Post by ExtremeGaming »

Sorry I missed the part about friends too. That will only work for the user. I'll look back at this later if no one has the answer for you by then.
<?php while(!$succeed = try()); ?>
Post Reply