简体   繁体   中英

Query within a while loop

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 :-)

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM