如果数据库字段为空;什么都没有”;如果那里有什么回声“;什么”;


If database field is empty echo "nothing" if something there echo "something"

正在寻找下面这段代码的解决方案。

    <?php
$nextfive_events = mysql_query("SELECT date_format(date, '%d/%m/%Y') AS formatted_date, title, location, regs FROM events WHERE status = 'ACTIVE'");
if(mysql_num_rows($nextfive_events) == 0) { echo "<p>No events coming up!"; } else {
echo "<table width='"600'" border='"0'" cellpadding='"2'" cellspacing='"2'" class='"greywritinglight'" align='"left'">
<tr align='"center'">
<td>Date</td>
<td>Name</td>
<td>Location</td>
<td></td>
</tr>";
$x=1;
while($next_row = mysql_fetch_array($nextfive_events))
{
  if($x%2): $rowbgcolor = "#FFFFFF"; else: $rowbgcolor = "#EEEEEE"; endif;
echo "<tr align='"center'">";
echo "<td>" . $next_row['formatted_date'] . "</td>";
echo "<td>" . $next_row['title'] . "</td>";
echo "<td>" . $next_row['location'] . "</td>";
echo "<td><a href='"regs/" . $next_row['regs'] . "'">Regs</a></td>";
echo "</tr>";
$x++;
}
echo "</table>";
}
?>

我想要echo "<td> <a href regs .....

当数据库中的"Regs"中有内容时,显示单词Regs。如果这个领域什么都没有,我希望它是空白的,而不是说Regs。

感谢

您可以执行三元运算符:

echo "<td><a href='" . (empty($next_row['regs']) ? "#" : $next_row['regs']) . "'>Regs</a></td>";

尽量不要使用转义符,它们看起来很混乱,对href属性使用单引号。另外,你想要吗

<a href='#'>Regs</a>

显示它是否为空白?

如果没有,试试这个:

echo (!empty($next_row['regs']) ? "<td><a href='" . $next_row['regs'] . "'>Regs</a></td>" : "");

您可以使用三元:

echo ( ! empty($next_row['regs'])) ? '<td><a href='"regs/" . $next_row['regs'] . "'">Regs</a></td>' : '<td>&nspb;</td>';

首先,我想向您展示PHP的一个非常方便的技巧,它允许您在不说echo的情况下将其回显到HTML中。只需在PHP代码中创建一个中断,在这中间,您放置的任何内容都将作为HTML进行响应。

至于返回的行数,您做得是正确的。请确保查询正确,建立了连接,并且SQL中没有逻辑错误。

抱歉,没有注意到您要检查该列是否为空!只需使用函数empty()。当您提供给它的数据为空时,此函数将返回true,因此只需将要检查的数据行中的列提供给它即可。

<?php
    // Lets say this is your database connection variable
    $connection = mysqli_connect(//Your info goes here);
    // This is the query you want to run
    $query = "SELECT something FROM somewhere WHERE something='$something_else'";
    // This is where you are storing your results of the query
    $data = mysqli_query($connection, $query);
    // Here, you can ask for how many rows were returned
    // In PHP, ZERO is evaluated to false. We can use a 
    // Short-hand boolean expression like this
    if (mysqli_num_rows($data))
    {
        // Because zero evaluates to false, we know
        // that if we get HERE that there ARE rows
        // We can break out of PHP and into our HTML 
        // by simply ending our PHP tag! Just remember
        // that you need to open it back up again though!
        ?>
            <!--You can put your HTML here-->
            <p>
                We found some rows that you might
                need to know about!
            </p>
        <?php
    }
    else
    {
        // But, if we get here, we know it evaluated as
        // FALSE and therefore no rows returned
        // Here's that HTML trick again
        ?>
            <!--HTML can go here-->
            <p>
                No rows returned!
            </p>
        <?php
    }
?>
相关文章: