0

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 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks i try to study this JOIN function of mysql, really thank you for your answer

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.