如何只显示非空行
我有一个小的 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'] . " | ";
}
?>
我想保留空行。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
为什么你不放
why u not put
尝试选择列名
FROM 表名
WHERE TRIM(column_name) IS NOT NULL
Try SELECT column_name
FROM table_name
WHERE TRIM(column_name) IS NOT NULL
您可以在回显该行之前放置一个
if(!empty($row['firstname']))
。You can put an
if(!empty($row['firstname']))
before you echo the row.Alex 的空检查很好,如果你想显示不完整的记录,你也可以内联检查所有三个:
Alex's empty check is good, and you can also check all three of them inline if you want to display incomplete records:
我认为在“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
.要仅打印结果
IS NOT NULL
的行的rowname
和结果,请尝试以下操作:To print out only
rowname
and result from rows where resultIS NOT NULL
, try this: