显示记录时如何检查字段为空

时间:2013-06-28 09:39:17

标签: php mysql if-statement

我希望PDF字段为空时隐藏PDF字段,我搜索此论坛和其他许多人,我读的越多,我就越困惑!

我之前和之后用====标记了相关区域!

这是我的代码:

<?php
$id = $_GET['id'];
if(empty($id))
    header('Location: news.php');
else
{
    mysql_connect('localhost','root','usbw');
    mysql_select_db('flexphpdir');
    $query = mysql_query("Select * From linkex WHERE linkexid = '$id'");
    if($query === false)
    {
        var_dump(mysql_error());
    }
    else
    {
        while($output = mysql_fetch_assoc($query))
        {
?>
            <video id="example_video_1" class="video-js vjs-default-skin" controls preload="none" width="320" height="184" data-setup="{}">
                <?php echo $output['title']; ?><br>
                <?php echo date('d-M-Y', $output['adddate']); ?><br>
                <?php echo $output['weekid']; ?>
                <source src="contentuploads/<?php echo $output['video']; ?>" type="video/mp4" />
            </video>
            //================================      
            <?php  if (get_field('pdf') != "")  { ?>
                //code if field has value ! = <?php echo $output['pdf']; ?> 
            <?php } else { ?>
                //code for no field value here
            <?php } ?>
            //=================================
<?php
        }
    }
?>
<?php } ?>

2 个答案:

答案 0 :(得分:1)

一种方法是使用is_null来检查字段:

if (is_null($output['pdf'])) 
{
    echo "No PDF";
}
else
{
    echo $output['pdf'];
}

if (is_null($output['pdf']))也可以写为if ($output['pdf'] === NULL)

Here is a great question that even have a comparative chart of what it will give you as result.

答案 1 :(得分:0)

有很多方法可以解决您的问题,请查看php manual .. 这里我使用isset()进行编码 试试这个

<?php
    if(isset($_POST['pdf'])){
       if($_POST['pdf'] == $output['pdf']){
           echo "<label></label>";
       }
    }
    else{
       echo "";//
    }
?>