从数据库回收“0”,不起作用

时间:2014-02-07 16:35:36

标签: php sql echo

我在数据库“blabla”中有列,它是varchar 55,utf8_general_ci,它的值是“0”,不是空的,而是数字零。

当我尝试我的代码时:

echo $row['blabla'];

它不会回应任何东西。

如果它是1或更多,或者是字母,它会回显。

我有什么改变,所以它实际上会回应“0”?

如果这是noob问题,我很抱歉^^

谢谢!

 $champions_q = $dbc->query("SELECT * FROM `champions` ORDER BY `id` ASC");
$champions_q->execute();
while($row = $champions_q->fetch(PDO::FETCH_ASSOC)){
    $champ_name = $row['name'];

  <td class="box"> <b> Total deaths:  
                    </b>  <?php if(empty($row['total_deaths']) === false){ echo '<b id="value_box">'.$row['total_deaths']; } ?> </b> 
                    <td width="15"></td></td>

总死亡数,在数据库中为0。

谢谢大家,解决了:)。

2 个答案:

答案 0 :(得分:1)

您当前的if与您想要达到的目标相反。如果$row['total_deaths']0,那么if(empty($row['total_deaths']) === false)肯定会失败,因为empty(0)为真。

尝试将if更改为以下内容:

$champions_q = $dbc->query("SELECT * FROM `champions` ORDER BY `id` ASC");
$champions_q->execute();
while($row = $champions_q->fetch(PDO::FETCH_ASSOC)){
    $champ_name = $row['name'];

  <td class="box">
  <b> Total deaths:</b>
  <?php
    if (empty($row['total_deaths']))
      echo '<b id="value_box">0</b>';
    else 
      echo '<b id="value_box">'.$row['total_deaths'].'</b>';
  ?>
  <td width="15"></td>
  </td>

If-else简写方式:

$champions_q = $dbc->query("SELECT * FROM `champions` ORDER BY `id` ASC");
$champions_q->execute();
while($row = $champions_q->fetch(PDO::FETCH_ASSOC)){
    $champ_name = $row['name'];

  <td class="box">
  <b> Total deaths:</b>
  <?php echo (empty($row['total_deaths']) ? '<b id="value_box">0</b>' : '<b id="value_box">'.$row['total_deaths'].'</b>'); ?>
  <td width="15"></td>
  </td>

答案 1 :(得分:0)

$champions_q = $dbc->query("SELECT * FROM `champions` ORDER BY `id` ASC");
$champions_q->execute();
while($row = $champions_q->fetch(PDO::FETCH_ASSOC)){
    $champ_name = $row['name'];

  <td class="box"> <b> Total deaths:  
                    </b>  <?php if($row['total_deaths'] != ''){ echo '<b id="value_box">'.$row['total_deaths']; } ?> </b> 
                    <td width="15"></td></td>