Oookay, I informed myself and tried some things...
I came up with that solution, which is quite limited, but it works... (somehow)
function searchNode($start, $end, $connection)
{
$sql = '
SELECT
r1.user_id AS node0,
r1.friend_id AS node1,
r2.friend_id AS node2,
r3.friend_id AS node3,
r4.friend_id AS node4
FROM relation r1
JOIN relation r2 ON r2.user_id = r1.friend_id
JOIN relation r3 ON r3.user_id = r2.friend_id
JOIN relation r4 ON r4.user_id = r3.friend_id
WHERE r1.user_id = \'%1$d\'
AND
( r2.user_id = \'%2$d\'
OR r2.friend_id = \'%2$d\'
OR r3.friend_id = \'%2$d\'
OR r4.friend_id = \'%2$d\' )
';
$sort = false;
if($start > $end)
{
$tStart = $end;
$end = $start;
$start = $tStart;
$sort = true;
}
$result = mysqli_query($connection, sprintf($sql, $start, $end));
while($row = mysqli_fetch_assoc($result))
{
if($sort == true)
$row = array_reverse($row);
echo '<pre>'.print_r($row, true).'</pre>';
}
}
searchNode(4, 1, $connection);
$connection expects a mysqli_connection link.
Some Testdata:
CREATE TABLE `relation` (
`user_id` int(11) NOT NULL,
`friend_id` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `relation` (`user_id`, `friend_id`) VALUES
(1, 3),
(2, 4),
(3, 7),
(4, 0),
(5, 6),
(6, 4),
(7, 6),
(7, 8),
(0, 0),
(8, 1);
It is far from perfect and I'm still testing it. No idea about its performance, wanted to get it fully working before comparing it to algorithms and other things.
Very open to comments and tips 