While inside another While (PHP - SQL)

1

I searched the forum and the internet, but I could not solve the problem yet! I am doing a query in the bank by PHP and looking for users. After searching the users I look for his academic formations within that other search (since there may be more than one) with this the second while it works only on the first user, the second on it looks like "to".

Here are my codes:

Search for users

$query_busca_prof = "SELECT * FROM tags, user WHERE tag_tag LIKE '%$busca%' AND tag_user = user_id";
$Busca_busca_prof = mysql_query($query_busca_prof, $bd) or die(mysql_error());
$row_busca_prof = mysql_fetch_assoc($Busca_busca_prof);
$totalRows_busca_prof = mysql_num_rows($Busca_busca_prof);

Print Users and Training

do { 

    $query_busca_forma = "SELECT * FROM user_prof_forma WHERE user_prof_forma_user = '".$row_busca_prof['user_id']."'";
    $Busca_busca_forma = mysql_query($query_busca_forma, $bd) or die(mysql_error());
    $row_busca_forma = mysql_fetch_assoc($Busca_busca_forma);
    $totalRows_busca_forma = mysql_num_rows($Busca_busca_forma);

    echo $row_busca_prof['user_nome'];

    do {

        echo $row_busca_forma['user_formacao'];

      } while ($row_busca_forma = mysql_fetch_assoc($Busca_busca_forma)); // este que não funciona

} while($row_busca_prof = mysql_fetch_assoc($Busca_busca_prof));

Thank you for your attention!

    
asked by anonymous 11.01.2016 / 13:02

3 answers

3

The command mysql_fetch_assoc() is the command that acquires a record from a executed query. It needs to be run iteratively:

while($row_busca_forma = mysql_fetch_assoc($Busca_busca_forma)){
    // Faça suas atividades aqui dentro
}

Your code only works for the first record exactly because you are only running mysql_fetch_assoc only once.

Recommended reading: Why should not I use mysql_ *

    
11.01.2016 / 13:09
1

When you declare in your while($row_busca_forma = mysql_fetch_assoc($Busca_busca_forma) it will only run once so that the result is equal = , so ... it will stop execution.

Try to put a sign of less than or equal to <= , for example;

while($row_busca_forma <= mysql_fetch_assoc($Busca_busca_forma)

    
11.01.2016 / 13:12
1

In fact the correct way to put several lines of a search within an array, is with a while, dessamaneira:

$query_busca_prof = "SELECT ...";
$result= mysql_query($query_busca_prof, $bd) or die(mysql_error());    

$array_content = array();

while ($row = mysqli_fetch_array($result, MYSQL_ARRAY))
{
    $array_content[] = $row;
}

The way you're doing, you just get the first line and put it in the array, you can check this by printing the array in question.

    
11.01.2016 / 13:09