Author Topic: need in getting data from db  (Read 443 times)

0 Members and 1 Guest are viewing this topic.

Offline doforumdaTopic starter

  • Enthusiast
  • Posts: 295
    • View Profile
need in getting data from db
« on: February 08, 2010, 09:23:54 PM »
Hi
i want to select data from db by comparing two things. First I want to select all the ids of currently logged in user's friends from friends table. then i want to select all the posts of currently logged in user's friends as well as his own posts from posts table. I am trying to achieve this task using following code but it does not work.
<?php 
session_start
();
$connect mysql_connect("localhost","user","pass");
mysql_select_db("test");



$friends mysql_query("SELECT * FROM friend WHERE userid='$_SESSION[userid]'");

while(
$friend mysql_fetch_assoc($friends)) {
	
$f $friend['friendid'];
	
//echo $f."<br />";
	

	
$query mysql_query("SELECT * FROM posts WHERE userid='$f' AND userid='$_SESSION[userid]'");
	

	
while(
$id mysql_fetch_assoc($query)) {
	
	
$userpost $id['post'];
	
	
$date $id['date'];
	
	
echo 
$userpost."<br />".$date."<br />";
	

	
}
	

}
?>

Offline doforumdaTopic starter

  • Enthusiast
  • Posts: 295
    • View Profile
Re: need in getting data from db
« Reply #1 on: February 08, 2010, 10:54:03 PM »
ok here are my friend and post tables

please help i am waiting for help

[attachment deleted by admin]

Offline doforumdaTopic starter

  • Enthusiast
  • Posts: 295
    • View Profile
Re: need in getting data from db
« Reply #2 on: February 09, 2010, 01:07:38 AM »
I am still waiting for help

Offline jl5501

  • Devotee
  • Posts: 728
  • Gender: Male
  • PHP Freelancer
    • View Profile
Re: need in getting data from db
« Reply #3 on: February 09, 2010, 03:17:39 AM »
It is possible that this could be done in one query, but, using the code you have, if you change the AND to OR in your second query then your results will probably be better.

currently you are expecting the field userid to have 2 different values at the same time.
Secure forms without Captcha

Offline doforumdaTopic starter

  • Enthusiast
  • Posts: 295
    • View Profile
Re: need in getting data from db
« Reply #4 on: February 09, 2010, 04:23:42 AM »
It is possible that this could be done in one query, but, using the code you have, if you change the AND to OR in your second query then your results will probably be better.

currently you are expecting the field userid to have 2 different values at the same time.
i tried with OR as well but still have not the same result which i wanted

Offline doforumdaTopic starter

  • Enthusiast
  • Posts: 295
    • View Profile
Re: need in getting data from db
« Reply #5 on: February 09, 2010, 04:29:46 AM »
It is possible that this could be done in one query, but, using the code you have, if you change the AND to OR in your second query then your results will probably be better.

currently you are expecting the field userid to have 2 different values at the same time.
i tried with OR as well but still have not the same result which i wanted

now i use this code
Code: [Select]
<?php 

$connect 
mysql_connect("localhost","user","pass");
mysql_select_db("test");



$friends "SELECT * FROM friend WHERE userid='$_SESSION[userid]'";
$result mysql_query($friends);
echo 
$friends."<br><br>";
while(
$friend mysql_fetch_assoc($result)) {
$f $friend['friendid'];
echo $f."<br />";

$query "SELECT * FROM posts WHERE userid='$f' OR userid='$_SESSION[userid]'";
$result2 mysql_query($query);
echo $query."<br><br>";
while($id mysql_fetch_assoc($result2)) {
$userid $id['post'];
$date $id['date'];
echo $userid."<br />".$date."<br />";
}

}
?>

this is what it displays
Code: [Select]
SELECT * FROM friend WHERE userid='1'

10
SELECT * FROM posts WHERE userid='10' OR userid='1'

This is post of user 1
2010-02-02
This is second post of User 10
2010-02-01
11
SELECT * FROM posts WHERE userid='11' OR userid='1'

This is post of user 1
2010-02-02
This is third post of user 11
2010-02-03
20
SELECT * FROM posts WHERE userid='20' OR userid='1'

This is post of user 1
2010-02-02
This is last post of user 20
2010-02-05

Offline jl5501

  • Devotee
  • Posts: 728
  • Gender: Male
  • PHP Freelancer
    • View Profile
Re: need in getting data from db
« Reply #6 on: February 09, 2010, 04:36:47 AM »
Yes ok the results are entirely what you would expect from what you are doing.

each time you call the second query you will get the results of the logged in user as well as the particular friend.

What you need to do, if using 2 queries, is to store all the ids you want plus the logged_in users id to , then query the posts against that list of ids
Secure forms without Captcha

Offline jl5501

  • Devotee
  • Posts: 728
  • Gender: Male
  • PHP Freelancer
    • View Profile
Re: need in getting data from db
« Reply #7 on: February 09, 2010, 04:42:35 AM »
I would think that a one query solution would work better for you, so try
Code: [Select]
select * from posts p,friend f where  (p.userid=f.userid or p.userid=f.friendid) and f.userid='.$_SESSION['userid']
Secure forms without Captcha

Offline doforumdaTopic starter

  • Enthusiast
  • Posts: 295
    • View Profile
Re: need in getting data from db
« Reply #8 on: February 09, 2010, 04:45:18 AM »
I would think that a one query solution would work better for you, so try
Code: [Select]
select * from posts p,friend f where  (p.userid=f.userid or p.userid=f.friendid) and f.userid='.$_SESSION['userid']
ok but i dont understand what are those p and f in this query?

Offline doforumdaTopic starter

  • Enthusiast
  • Posts: 295
    • View Profile
Re: need in getting data from db
« Reply #9 on: February 09, 2010, 04:57:33 AM »
i am trying your query but i think i am putting it at wrong place can you please modify my abit?

Offline doforumdaTopic starter

  • Enthusiast
  • Posts: 295
    • View Profile
Re: need in getting data from db
« Reply #10 on: February 09, 2010, 05:02:35 AM »
i am trying your query this way
Code: [Select]
$friends = "select * from posts p,friend f where (p.userid=f.userid or p.userid=f.friendid) and f.userid='$_SESSION[userid]'";
$result = mysql_query($friends);
echo $friends."<br><br>";
while($friend = mysql_fetch_assoc($result)) {

$userid = $friend['post'];
$date = $friend['date'];
echo $userid."<br />".$date."<br />";


}

it displays this
Code: [Select]
select * from posts p,friend f where (p.userid=f.userid or p.userid=f.friendid) and f.userid='1'

This is post of user 1
2010-02-02
This is second post of User 10
2010-02-01
This is post of user 1
2010-02-02
This is third post of user 11
2010-02-03
This is post of user 1
2010-02-02
This is last post of user 20
2010-02-05
« Last Edit: February 09, 2010, 05:03:41 AM by doforumda »

Offline doforumdaTopic starter

  • Enthusiast
  • Posts: 295
    • View Profile
Re: need in getting data from db
« Reply #11 on: February 09, 2010, 06:52:43 AM »
my problem is still unsolved