[英]PHP / SQL if null do something else something else
I would like to check if a particular value in a table is set to null so as to echo information accordingly. 我想检查表中的特定值是否设置为null,以便相应地回显信息。 I would like that if the $row['GeneralID']
is set to null a particular code is printed, and if not another is printed, as this would affect the way things are displayed. 我希望如果将$row['GeneralID']
设置为null,则将打印特定的代码,如果不打印其他代码,则这将影响显示内容的方式。
function responsive_menu($sql) {
include 'connect.php';
$result = $conn->query($sql);
if ($result->num_rows > 0);
$current_album = "";
echo "<div id='responsive_menu'>\n<div id='show_menu'>\n<img src='media/images/show-menu-icon.png' alt='menu_icon'/>\n</div>\n<div id='cssmenu'>\n<ul>\n";
while ($row = $result->fetch_assoc()) {
if ($current_album <> "" && $current_album <> $row['pageID']) {
if ($row['GeneralID'] <> NULL) {
echo "</ul></li>\n";
}
else {
echo "";
}
}
if ($current_album <> $row['pageID']) {
if ($row['GeneralID'] <> NULL) {
echo "<li class='has-sub'><a href='".$row['pageURL']."?pageID=".$row['pageID']."'><span>".$row['page_name']."</span></a>\n<ul>\n";
}
else {
echo "<li><a href='".$row['pageURL']."?pageID=".$row['pageID']."'><span>".$row['page_name']."</span></a>\n</li>\n";
}
$current_album = $row['pageID'];
}
if ($row['GeneralID'] == NULL) {
echo "";
} else {
echo "<li><a href='info.php?pageID=".$row['pageID']."&infoID=".$row['GeneralID']."'><span>".$row['general_name']."</span></a></li>\n";
}
}
echo "</ul>\n</div>\n</div>\n";
$conn->close();
}
Here is the resulting code on top and the way the code should be displayed at the bottom: http://jsfiddle.net/yevfc1rw/ 这是最上面的结果代码,最下面的代码显示方式: http : //jsfiddle.net/yevfc1rw/
You need 3 equal signs: 您需要3个等号:
if( $row['GeneralID'] === NULL ) //do stuff
PHP treats NULL, false, 0, and the empty string as equal when using 2 equal signs. 当使用2个等号时,PHP将NULL,false,0和空字符串视为相等。
3 signs will ensure to check type and value 3个标志将确保检查类型和价值
The <>
operator is not valid in PHP, you should use !=
instead, but nevertheless PHP has a function for checking if a value is null, is_null()
. <>
运算符在PHP中无效,您应该改用!=
,但是PHP具有用于检查值是否为null的函数is_null()
。 Therefore instead of $row['GeneralID'] <> NULL
you should use; 因此,应使用$row['GeneralID'] <> NULL
代替;
!is_null($row['GeneralID'])
ie, $row['GeneralID']
is not null. 即$row['GeneralID']
不为null。 The !
!
means NOT. 表示不。
Or for checking for the varible is null, instead of $row['GeneralID'] == NULL
; 或者用于检查变量是否为null,而不是$row['GeneralID'] == NULL
;
is_null($row['GeneralID'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.