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!
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.
– rray
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 themysql_num_rows
– RFL
@rray did not understand very well what you meant!
– Leonardo
Do as rray said, if you use Join vc can avoid these unnecessary whiles
– DiChrist
I’ll put the answer and Voce confirms ok @Leonardocarmo ?
– RFL
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...
– DiChrist
@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
@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.
– Daniel Omine
@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
– Leonardo
post the selects there.
– Daniel Omine
@Danielomine I edited there!
– Leonardo
did not understand the excerpt
'%$busca%' AND tag_user = user_id
. The fielduser_id
comes from where? It wouldn’t be a PHP variable?– Daniel Omine