如果mysql任何字段为空,则显示一个像null的单词

时间:2016-01-15 21:19:43

标签: php html mysql

我需要知道,如果有任何空字段,则在检索表单中显示null。我的意思是,

名---------- ----------年龄国家

XYZ ----------" " --------- USA

所以,表格会显示

name:xyz

age:show null

country:usa

<?php
$dbhost = '';
$dbuser = '';
$dbpass = '';


$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn ) {
  die('Could not connect: ' . mysql_error());
}
$No=$_GET['No'];
$sql = "SELECT * from tablename where Name='$Name'";
mysql_select_db('dbname');
$retval = mysql_query( $sql, $conn );

if(! $retval ) {
  die('Could not get data: ' . mysql_error());
 }

while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {

   ?>


 <table width="100%"><tr><td>
 <table>
 <tr>
 <tr><td class="a" style="width:20%">Name</td><td style="width:20%"  class="a"><a class="res" ><?php echo $row['Name'];?></a></td></tr>
 <tr><td class="a" style="width:20%">age</td><td style="width:20%"   class="a"><a class="res"><?php echo $row['age'];?></a></td></tr>
 <tr><td class="a" style="width:20%">country</td><td style="width:20%"  class="a"><a class="res"><?php echo $row['country'];?></a></td></tr>
 </tr>
 </table>

 <?php
 }


    mysql_close($conn);
 ?>`

2 个答案:

答案 0 :(得分:1)

只测试结果是否为空以及是否显示为空。

<?php if ($row['Name'] != '') {
    echo $row['Name'];
} else {
    echo "Null";
}?>

答案 1 :(得分:0)

echo empty($row['age']) ? 'null' : $row['age']

PS:避免SQL注入!