php if语句检查字段是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11778340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php if statement to check if field is empty
提问by James
Possible Duplicate:
How to check if mysql entry is empty in PhP?
I'm trying to run a seemingly simply if statement, however, my PHP skills are not that great.
我试图运行一个看似简单的 if 语句,但是,我的 PHP 技能并不是那么好。
I am querying data from a single MySQL table and I want to run a statement that detects if a field is empty or not, if it's not I want it to display something, if it is, to not display anything.
我正在查询单个 MySQL 表中的数据,我想运行一个语句来检测一个字段是否为空,如果不是,我希望它显示某些内容,如果是,则不显示任何内容。
Also, I'm not sure if it's possible to put this in an echo or not?
另外,我不确定是否可以将其放入回声中?
EDIT:************************ Here's the code to make things easier:
编辑:************************ 下面是使事情更容易的代码:
I may have done something wrong. Here is my code to explain things a little easier
我可能做错了什么。这是我的代码,可以更轻松地解释事情
'
'
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
//creating the table w/ headers
echo '
<table>
<thead>
<tr>
...
<th style="width:90px"><strong><a href="other-items.php?order=oc_retailer">Retailer</a></strong></th>
<th style="width:90px"><strong>Files</strong></th>
</tr>
</thead>';
// Display each item
while ($row = mysql_fetch_array($result)) {
echo '
<tr>
...
<td style="width:90px">' . $row['oc_retailer'] .'</td>
<td style="width:90px"> this is where I want to if statement to go</td>
';
}
echo '</table>';
?>'
回答by Fluffeh
You can use the empty()function which will check to see if anything is there.
您可以使用该empty()功能来检查是否有任何东西存在。
The isset()checks for a null (boolean==false) on the field - which you can also use.
在isset()这你也可以使用-为场上空(布尔==假)检查。
if(empty($row['yourColumn']))
{
// it's empty!
echo "Nada in the field!.\n";
}
else
{
// Do stuff with the field
}
Edit: I think this is what you mean:
编辑:我认为这就是你的意思:
while ($row = mysql_fetch_array($result))
{
if(!empty($row['oc_retailer']))
{
echo '
<tr>
...
<td style="width:90px"> this is where I want to if statement to go</td>
';
}
else
{
echo '
<tr>
...
<td style="width:90px">Ruh ohes! The Row was Empty!</td>
';
}
}
回答by Berry Langerak
I am querying data from a single MySQL table and I want to run a statement that detects if a field is empty or not, if it's not I want it to display something, if it is, to not display anything.
我正在查询单个 MySQL 表中的数据,我想运行一个语句来检测一个字段是否为空,如果不是,我希望它显示某些内容,如果是,则不显示任何内容。
So, you have a resultset which is an array, I reckon? In which case you'd want to check that the value is not equal to an empty string:
所以,你有一个结果集,它是一个数组,我想?在这种情况下,您需要检查该值是否不等于空字符串:
if($resultset['fieldname']===' '){
echo 'fieldname was empty';
}
Please mind that the answer with isset is actually invalid: the value will always be set, because the field exists in the resultset, it's just an empty string by default.
请注意, isset 的答案实际上是无效的:该值将始终被设置,因为该字段存在于结果集中,默认情况下它只是一个空字符串。
EDIT: I was mistaken about the empty( )call, it appears to check for isset($var) && !$var. Still, because you have control over what columns you query, there's no need for the isset part and I'd advice against using empty( )in this situation.
编辑:我对empty( )电话有误解,它似乎检查isset($var) && !$var. 尽管如此,因为您可以控制查询的列,所以不需要 isset 部分,我建议不要empty( )在这种情况下使用。
回答by Sourav
After getting the data from MySQL try
从 MySQL 获取数据后尝试
if (!isset($data[0]))
echo 'Input some data';
or
或者
if (strlen($data[0])<1)
echo 'Input some data';
回答by lusketeer
If you using mysql_query, you can use
如果你使用mysql_query,你可以使用
if (mysql_num_rows($result) > 0) {
echo "There's Data";
}
Check mysql_num_rows()
But it's better to just use prepared statement.
但最好只使用准备好的语句。
PDO uses PDO::rowCount()
PDO 使用PDO::rowCount()
Mysqli uses num_rows
Mysqli 使用num_rows

