如何只显示非空行

发布于 2024-10-19 06:41:43 字数 583 浏览 4 评论 0原文

我有一个小的 PHP mysql 查询,查看数据库中的表。它会拉出所有行,然后回显记录。

问题是我的表中有一些空行,所以我得到的是一个看起来很奇怪的记录。

这就是我得到的:

<?php
    $con = mysql_connect("localhost","username","password");
    if (!$con){
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("username", $con);
    $result = mysql_query("SELECT * FROM wallnames2011 WHERE firstname IS NOT NULL");
    while($row = mysql_fetch_array($result)){
        echo $row['firstname'] . " - " . $row['city'] . ", " .$row['state'] . " &nbsp;|&nbsp; ";
    }
?>

我想保留空行。

I have a small PHP mysql query looking at a table in my database. It's pulling all rows and then echoing out the record.

Problem is I have a few empty rows in the table so what I get is a weird looking record.

This is what I got:

<?php
    $con = mysql_connect("localhost","username","password");
    if (!$con){
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("username", $con);
    $result = mysql_query("SELECT * FROM wallnames2011 WHERE firstname IS NOT NULL");
    while($row = mysql_fetch_array($result)){
        echo $row['firstname'] . " - " . $row['city'] . ", " .$row['state'] . "  |  ";
    }
?>

I want to keep the empty rows out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

孤檠 2024-10-26 06:41:43

为什么你不放

尝试 SELECT column_name FROM table_name WHERE column_name!=''

why u not put

Try SELECT column_name FROM table_name WHERE column_name!=''

醉态萌生 2024-10-26 06:41:43

尝试选择列名
FROM 表名
WHERE TRIM(column_name) IS NOT NULL

Try SELECT column_name
FROM table_name
WHERE TRIM(column_name) IS NOT NULL

悲欢浪云 2024-10-26 06:41:43

您可以在回显该行之前放置一个 if(!empty($row['firstname']))

You can put an if(!empty($row['firstname'])) before you echo the row.

多彩岁月 2024-10-26 06:41:43

Alex 的空检查很好,如果你想显示不完整的记录,你也可以内联检查所有三个:

echo (! empty($row['firstname']) ? $row['firstname'] : "")
     .(! empty($row['city']) ? " - ".$row['city'] : "")
     .(! empty($row['state']) ? ", ".$row['state'] : "")
     . "  |  ";

Alex's empty check is good, and you can also check all three of them inline if you want to display incomplete records:

echo (! empty($row['firstname']) ? $row['firstname'] : "")
     .(! empty($row['city']) ? " - ".$row['city'] : "")
     .(! empty($row['state']) ? ", ".$row['state'] : "")
     . "  |  ";
零度° 2024-10-26 06:41:43

我认为在“where”子句中提到了所有字段名称,例如first_name IS NOT NULL AND last_name IS NOT NULL

I think in the "where" clause mention all field names like first_name IS NOT NULL AND last_name IS NOT NULL.

鲸落 2024-10-26 06:41:43

要仅打印结果 IS NOT NULL 的行的 rowname 和结果,请尝试以下操作:

if ($result = $mysqli->query($query)) {
    /* fetch associative array */
    $i=1;
    while($a = $result->fetch_assoc()) {
        $i++;
        foreach ($a as $key => $value) {
            if($value != NULL) {
                echo $key." = ".$value."<br/>";
            }
        }
        echo "-- <br />";
    }
    /* free result set */
    $result->free();
}
/* close connection */
$mysqli->close();

To print out only rowname and result from rows where result IS NOT NULL, try this:

if ($result = $mysqli->query($query)) {
    /* fetch associative array */
    $i=1;
    while($a = $result->fetch_assoc()) {
        $i++;
        foreach ($a as $key => $value) {
            if($value != NULL) {
                echo $key." = ".$value."<br/>";
            }
        }
        echo "-- <br />";
    }
    /* free result set */
    $result->free();
}
/* close connection */
$mysqli->close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文