Your problem is caused by a missing '}'. For example, this closing '}' brace:
} //fin while pour $row
actually matches up with the '{' opening brace on this line:
if (mysql_num_rows($result2)>0) { //nbre d'enregistrements retournés par $query2
Since the code comment suggests that it should have matched up with the while() opening brace, I'm guessing that the closing '}' brace should go before the one referenced above.