0

I have this query call:

$currplayer = 1;

$query = "SELECT f.fk_user_id2, u.username, u.profileimg FROM ".$prefix."_friends f 
                        INNER JOIN ".$prefix."_users u
                            ON f.fk_user_id2 = u.id     
                    WHERE f.fk_user_id1 = $currplayer ORDER BY u.username ASC";

if ($result = $mysqli->query($query)){
    while ($row1 = $result->fetch_assoc()){

        $friendid = $row1['fk_user_id2'];

        $sql = $mysqli->query("SELECT * FROM ".$prefix."_newversus WHERE fk_player=$currplayer AND fk_opponent=$friendid");
        $row = $sql->fetch_assoc();

I am trying to figure out how I get the query within the loop into the first query with inner join, but can't figure this out.

hoping for help and thanks in advance :-)

1 Answer 1

1

If i got you corrrectly, you want to remove the query which is running in loop & add it to main query.

Try this:

SELECT f.fk_user_id2, u.username, u.profileimg FROM ".$prefix."_friends f 
INNER JOIN ".$prefix."_users u ON f.fk_user_id2 = u.id 
INNER JOIN ".$prefix."_newversus n ON f.fk_user_id2 = n.fk_opponent 
WHERE f.fk_user_id1 = $currplayer AND
n.fk_player=$currplayer
ORDER BY u.username ASC;
Sign up to request clarification or add additional context in comments.

1 Comment

Almost correct, but... This will only get opponents which i have a versus against... I also want the opponents which I dont have a newversus record against?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.