PHP mysqli extract data from different tables and

2019-09-14 11:44发布

问题:

I'm stuck coding a part of a website. I'm extracting from the first table the friendship for the account you are using.

Then I make a while to get all usernames of my friends then I query the profiles table to take out other two information like sex and birth date.

My problem is that the first if(query) and while(friend_username) works fine and I can take out all the data and print the list of users but when I do the second query with the second while(sex,birth_date) nothing works and I cant find the problem it's the second day I try to fix it but I can't find a solution..

Sorry for my bad English and also sorry for my bad coding skills.

I don't know what I'm doing wrong.

<?php

$query = "SELECT friend_username FROM friends WHERE username = ? AND amicizia = '1' AND ban = '0' ";
if($stmt = $mysqli->prepare($query))
{
    // bind parameter
    $stmt->bind_param('s', $username );
    // execute
    $stmt->execute();
    // bind result to a variable
    $stmt->bind_result($amici);
    // iterate through the results
    echo "$amici";
    while ($stmt->fetch())
    {
        $queryu = "SELECT sesso , data_nascita FROM profiles WHERE username = ? ";
        if($stmtp = $mysqli->prepare($queryu))
        {
            // bind parameter
            $stmtp->bind_param('s', $amici );
            // execute
            $stmtp->execute();
            // bind result to a variable
            $stmtp->bind_result($sesso, $data_nascita);
            while ($stmtp->fetch())
            {
                // FARE CICLO WHILE CHE ESTRAPOLA LE INFORMAZIONE DALLA TABELLA PROFILES MA SISTEMARE PRIMA HE AL LOGIN COMPILI I CAMPI SULLA TAB PROFILES
                echo "
                <tr>
                <td>
                <div class=\"gallery\" style=\"height: 76px;\">
                <a href=\"img/profilo.jpg\"><img class=\"align-left\" src=\"img/amico.png\" width=\"60\" height=\"60\" /></a>
                </div>
                </td>
                <td>
                <ul style=\"height:45px;\">
                <li style=\"padding: 0;\">$amici</li>
                <li style=\"padding: 0;\">$sesso</li>
                <li style=\"padding: 0;\">$data_nascita</li>
                </ul>
                </td>
                <td>
                <p>Lorem Ipsum è un testo segnaposto utilizzato nel settore della tipografia e della stampa. Lorem Ipsum è considerato il testo segnaposto standard sin dal sedicesimo secolo</p>
                </td>
                <td>
                <a href=\"\"><i class=\"icon-user icon-2x\"></i></a>
                <a href=\"\"><i class=\"icon-envelope icon-2x\"></i></a>
                <a href=\"\"><i class=\"icon-heart-empty icon-2x\"></i></a>
                <a href=\"\"><i class=\"icon-trash icon-2x\"></i></a>
                </td>
                </tr>
                ";
            }
        }
    }
}

?>

回答1:

If possible, you should use a table join to fetch all required data in one query. Using the tables from your code snippet it could look like this:

SELECT a.friend_username, b.sesso, b.data_nascita FROM friends as a 
JOIN profiles as b ON(b.username=a.friend_username)
WHERE a.username = ? AND a.amicizia = '1' AND a.ban = '0'

To answer your actual question, it is possible to do nested queries but for your example to work you would need to create a second mysql connection (ex: $mysqli2 = new mysqli(...)), and use that in the outer while loop.



标签: php mysqli