如果行不为空且不为空,如何显示该行?
问题描述:
I have a page that displays various fields from a mysql_query. I only want to display the following field/row when the value is available.
$web = $row['Website'];
<?php
if ($web = NULL)
{?>
<!-- DO NOTHING -->
<?php
}
else
{?>
<tr>
<td valign='top' colspan='3' align='center'><DIV CLASS='standings_title'><?php echo $web ?></div></td>
</tr>
<?php
}
?>
Hopefully someone can tell me what I am doing wrong.
我有一个页面显示mysql_query中的各个字段。 我只想在值可用时显示以下字段/行。 p>
$ web = $ row ['Website'];
&lt;?php
if($ web = NULL)
{?&gt;
&lt;! - DO NOTHING - &gt;
&lt;?php
}
else
{?&gt;
&lt; tr&gt;
&lt; td valign ='top'colspan ='3'align ='center'&gt;&lt; DIV CLASS ='standings_title'&gt;&lt;?php echo $ web?&gt;&lt; / div&gt;&lt; / td&gt;
&lt; ; / tr&gt;
&lt;?php
}
?&gt;
code> pre>
希望有人可以告诉我我做错了什么。 p> \ n div>
答
<?php if($web):?>
<tr>
<td valign='top' colspan='3' align='center'><DIV CLASS='standings_title'><?php echo $web ?></div></td>
</tr>
<?php endif; ?>
答
Use isset()
.
if(isset($web))
{
}
Or
if($web)
{
}
答
$web = $row['Website'];
// Trim whitespace so a single blank space doesn't display as a populated value.
$web = trim($web);
if (!empty($web) {
// Display it.
}
答
if (isset($value) && strlen($value) > 0) {
//stuff
}
This is better than using empty($value) or just a if($value) because php considers things such as "0" as empty. However, if you can guarantee that value will never be "0", they are functionally equivalent, and !empty($value) will be faster.
From the php manual:
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
答
Post column of the row if is not null (in PDO):
$sql = 'SELECT * FROM posts where post_date !="" ';
$stmt = $database->prepare($sql);
$stmt->execute();
$rowCount = $stmt->rowCount();
if($rowCount < 1)
{
return null;
}
else
{
do somthing ...
}