Here is a snippet of code.

for ($g =0; $g++<$idg;) { $ch = mysql_query("SELECT chips FROM reit WHERE id_games='$g' AND id_users='1' ",$db); while ($chg = mysql_fetch_array($ch)) { $chg = $chg['chips']; echo "<td>$chg </td>"; } } 

When it is executed, it displays a column of chips (323, 5454, 45345, 43234) , but, let's say, to do id_games and id_users where there are no chips , it will output the same, only without a column, where there was no data. And how to make it so that when he sees that there is nothing, he changed nothing to 0 in the $chg variable? If you disassemble the cycle, it turns out like this:

 select chips from reit where id_users = '1' and id_games = '1'; 43432 select chips from reit where id_users = '1' and id_games = '2'; пусто select chips from reit where id_users = '1' and id_games = '3'; 434234 select chips from reit where id_users = '1' and id_games = '2'; 987 

But he will print 43432 434234 987. At the same time, the entire table collapses. Tried to make conditions, but it doesn't work.

 if ($chg == '') { $chg = '0'; } 
  • one
    select coalesce(chip, 0) FROM reit... - alexlz

2 answers 2

 select IFNULL(chips, 0) AS chips from reit 

    Try for the case if the chips have an empty value:

     $chg = $chg['chips']?$chg['chips']:0; 

    or in request:

     SELECT if(chips,chips,0) as chips FROM reit WHERE id_games='$g' AND id_users='1' 

    upd: if zero sampling then, as @alexlz correctly comments

     SELECT coalesce(chip, 0) FROM reit WHERE id_games='$g' AND id_users='1' 
    • something still doesn't work ((( - Alexander666
    • Most likely this is not the case. And we need to analyze mysql_num_rows ($ ch). If 0 - nothing is found, then ... php.net/manual/ru/function.mysql-num-rows.php And coalesce is unlikely to help here. That would be if the string was, and the value of chips for it was NULL - tads oh. - alexlz
    • @ Alexander666 with a nick there, and that worked? - alexlz
    • Rephrase - Alexander666
    • So 666 hinders. And does not go with what option? coalesce? see my comment above. - alexlz