如何在MySQL中只打印表中的非零值?

时间:2013-02-24 01:20:04

标签: php html mysql record

所以,例如,我有一个来自我的MySQL的表,带有一个记录 a

 | col1 | col2 | col3 |
a|  0   |   1  |  2   |

我想在PHP中回显这条记录,但我不希望打印出零值的列。所以它看起来像这样。

 | col2 | col3 |
a|  1   |  2   |

代码

 $get = "INSERT INTO table (col1, col2, col3) VALUES('0', '1', '2')";
 $result = mysql_query($get);

 echo "<table><tr><td>col2</td><td>col3</td></tr>";

  while ($row = mysql_fetch_array($result)) {
    echo"<tr> <td>".$row['col2']." </td>
    <td>".$row['col3']."</td>
    </tr>";
}

这是手动执行代码,但如果我有几十列,我希望代码检测哪些列有零,我不熟悉。

是的,基本上,我不知道允许我循环遍历MySQL中给定表的所有列的语法。我在PHP中使用MySQL

1 个答案:

答案 0 :(得分:0)

foreach($result_array as $key => $value)
{
    if ($value!=NULL)
    printf("<td>%s</td>", $value);
}