I pat the farm, the class $ db is used, I can’t stupidly execute the query inside the loop, the data is interrupted by the query inside while

<? $db->Query("SELECT * FROM db_task WHERE moder = '1' AND kol_z >= 1 $catt ORDER BY up_date DESC"); while($task = $db->FetchArray()) { $db->Query("SELECT id FROM db_task_user WHERE id_task = '$task[id]' LIMIT 1"); // эта штука перебивает похоже $db и выводит пустоту, на самом деле данные есть if($db->NumRows() == 0) {//если это и строку выше закомментить, всё норм ?> Norm<br> <? } } ?> 
  • Who taught you there to write like this <? ?> <? ?> - Naumov
  • @Naumov script is not mine, I know that govnokod, is there a solution? - Rammsteinik
  • @Rammsteinik, rewrite the database request so that there is only one request outside the loop. - Visman
  • one
    @Rammsteinik What about LEFT JOIN? :) - Firepro
  • one
    @Rammsteinik Of course, where should not be such conditions. Or in where write (B.task_user = 1 OR B.task_user is null) that all records would be. The same applies to the status field, which also needs to be checked for NULL. Or, generally, transfer the condition to ON: select * from db_task A left join db_task_user B on B.id_task=A.id and B.task_user = 1 and B.status<>1 - Mike

1 answer 1

Judging by $db->FetchArray() , this is one of the childhood diseases of all classwriters - state preservation .

$db saves the result of the query inside itself, when called in a loop, this variable is overwritten, and as a result, everything rises.

Within this class, the problem is not treated.
The solution is to throw out this self-made scribble and use PDO.

Although of course the subquery is evil in any case, and it should be rewritten to join.

  • I understood this a long time ago, when I saw the class, using it there is no solution? - Rammsteinik
  • @ Ipatiev, mysql_* has such a problem :) - Visman
  • Just added a line about it. You can forget about subqueries. the only option is to rewrite to join as advised above. - Ipatiev
  • @Visman never. Do not confuse the magic connection with the database (which is there) with the persistent result of the query (which is not there and never has been). The result is always explicitly stored in a resource type variable and overwritten only if the programmer does it himself. - Ipatiev
  • one
    @Visman If inside the loop by mysql_fetch_assoc guess to assign the result of another query to a variable with the same name , then yes - the loop will break;) - Ipatiev