user3437098
user3437098

Reputation: 29

Not select only first row in column

functions.php

function getfriendone($id, $field){
$query = mysql_query("SELECT `$field` FROM `friends` WHERE `user_one`='$id'");
$run = mysql_fetch_array($query);
return $run[$field];
}

function getfriendtwo($id, $field){
$query = mysql_query("SELECT `$field` FROM `friends` WHERE `user_two`='$id'");
$run = mysql_fetch_array($query);
return $run[$field];
}

index.php

$fetch_friends = $_SESSION['user_id'];
$two = getfriendone($fetch_friends, 'user_two');
$three = getfriendtwo($fetch_friends, 'user_one');

$result = mysql_query("SELECT * FROM posts WHERE id IN ('$two', '$three')");
echo $two." | ".$three;

Table (friends) for getfriend function

ID    user_one    user_two
1     1           2
2     1           3
3     4           1

Table (posts) for $result

ID    TEXT    NAME
1     Hello   Bob
1     Hello   Bob
2     Hello   Mark

What i wanna do here is echo all the rows that has integer 1 in it in both user_one and user_two. In my chase, only ID 1 and 3 will be echoed here. It picks the first match and echos only that one. It works as it should, but only with the first match in the column

Upvotes: 0

Views: 132

Answers (1)

Marco Mura
Marco Mura

Reputation: 582

If i read properly:

SELECT *
FROM posts s
WHERE 
EXISTS 
(SELECT 1 FROM posts s2 WHERE s.id = s2.user_one) OR
(SELECT 1 FROM posts s2 WHERE s.id = s2.user_two)

Will extract the data you need

SQLFiddle -> http://sqlfiddle.com/#!2/9ae070/3/0

Upvotes: 1

Related Questions