行sql为null,如何解决?

时间:2017-12-09 18:22:19

标签: php sql mysqli

<?php
    $sql = "(SELECT dari FROM mapping WHERE author = '$_SESSION[user]')
    UNION
    (SELECT ke FROM mapping WHERE author = '$_SESSION[user]')";

    $run = mysqli_query($conn, $sql);

    while ($rows = mysqli_fetch_assoc($run)) {
    if(!empty($rows['dari'])) {?>
        <option value="<?php echo $rows['dari']; ?>"><?php echo $rows['dari']; ?></option> <?php
        } else {?>
            <option value="A1">A1</option><?php
        } 
    }
 ?>
带有值A1的

菜单选项不能以

的形式出现

1 个答案:

答案 0 :(得分:0)

您需要至少输入一次

do 
{
    $rows = mysqli_fetch_assoc($run);
    if (null !== $rows && !empty($rows['dari']))
    { ?>
        <option value="<?php echo $rows['dari']; ?>"><?php echo $rows['dari']; ?></option> <?php
    }
    else
    { ?>
        <option value="A1">A1</option><?php
    }
} while ($rows);