While inside another While (PHP - SQL)

Asked

Viewed 1,439 times

1

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

They follow my code:

Search 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!

  • 2

    A Join or more won’t kill these chained whiles? Don’t use obsolete functions or you’ll get the trouble to stun the code later.

  • 1

    it seems that the error is in while, Voce speaks to carry out the action when it is = I mean, only 1x and it will look the same, try to replace with < or even do the while with a count using the mysql_num_rows

  • @rray did not understand very well what you meant!

  • Do as rray said, if you use Join vc can avoid these unnecessary whiles

  • 1

    I’ll put the answer and Voce confirms ok @Leonardocarmo ?

  • In its variable "$query_busca_prof" is the sql query. Use Join in it for you to avoid these while, it is logical that you will have to change the code above...

  • 1

    @Diegodesouzasilva It is that I simplified the html, then Join would not solve, because the structure I need to print separately! But now I understand

  • @Leonardo, rray meant that a more appropriate way is an SQL query with JOIN (LEFT JOIN, RIGHT JOIN, etc. ). So I would eliminate these loops of repetition.

  • @Danielomine got it! And if I put my selects you think you could help me? Because I don’t know how I could make a single select and work with differentiated html for each result after, and they can be more than one

  • post the selects there.

  • @Danielomine I edited there!

  • did not understand the excerpt '%$busca%' AND tag_user = user_id. The field user_id comes from where? It wouldn’t be a PHP variable?

Show 7 more comments

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 the mysql_fetch_assoc only once.

Recommended reading: Why should I not use mysql_*

  • Thanks for the recommendation, I’ll read!

1

In reality the correct way to place multiple lines of a search inside 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 are doing, you just take the first line and put it in the array, you can check this by printing the array in question.

  • I used it this way but it’s not printing anything!

  • @Leonardocarmo in your case, you would have to do this for the two while you did... It’s just that I’m at work now, but I’m going to rewrite the code to better handle your situation.

1

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

Try to put a smaller or equal sign <=, for example;

while($row_busca_forma <= mysql_fetch_assoc($Busca_busca_forma)

  • Solved exactly as I needed! Thanks!

  • @Leonardocarmo, mark as correct answer please.

  • I need to wait 5 m!

  • Now that I tested more I saw, in fact it did not solve 100%! If it has 2 records it does not put the correct ones, it repeats the same twice!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.