如果 isset 无法正常使用数据库提取
if isset not working properly with database fetch
所以我正在使用这个查询从我的数据库中获取一些信息。
$fetch_menu = $db->prepare("SELECT id, title, link, custom_link, rel FROM header_menu ORDER BY id DESC");
$fetch_menu->execute();
$header_menu = $fetch_menu->fetchAll();
$header_count = $fetch_menu->rowCount();
现在我正尝试使用此语句从我的数据库中回显数据。
$nav_count = 0;
while ($header_count > $nav_count){
echo "<li><a ";
if (isset($header_menu[$nav_count]['rel'])){
echo "rel='";
echo $header_menu[$nav_count]['rel'];
echo "'";
}
$nav_count ++;
}
现在,问题出现在我的网站上,我得到了这个输出(来自一个没有 rel 填充或 custom_url 填充的字段):
<a rel="" href="">Contact</a>
rel 的列在我的数据库中是空的,但它仍然回显。
我对这条语句有完全相同的问题,其中 custom_link 字段填充在菜单项的 1 个中,但其余的没有。
if (isset($header_menu[$nav_count]['custom_link'])){
echo " href='";
echo $header_menu[$nav_count]['custom_link'];
}
else if (isset($header_menu[$nav_count]['link'])){
echo " href='/";
echo $header_menu[$nav_count]['link'];
}
填充有 custom_url 的字段的输出是这样的:
<a rel="" href="http://example.com">Test page</a>
为什么我不能在我的数据库提取中使用 isset?
你可以使用 var_dump($header_menu);
查看变量中的内容。
也使用 empty() 而不是 isset()
如果您使用 mysqli,正如您用标签指明的那样,fetchAll 的工作方式类似于 mysqli_fetch_row,它将变量设置为查询返回的相应值,但 'sets NULL fields to the PHP NULL value'。所以(正如其他人在他们的评论中指出的那样)变量总是被设置,但请注意它的值可以是 NULL。
isset determines if 'a variable is set and is not NULL'. So actually, you can use isset, to determine if the query returns NULL for a field. But this is not your case, you have an empty string and you can use empty 检查一下。请注意,您不需要将 empty 与 isset 结合使用:'a variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.'
你的情况:
isset($header_menu[$nav_count]['rel']) // true, unless rel is NULL in the database
$header_menu[$nav_count]['rel'] == "" // true if rel is an empty string ("") in the database, false otherwise
empty($header_menu[$nav_count]['rel']) // true if rel is an empty string, "0" (as a string), 0, 0.0, FALSE or NULL in the database, false otherwise
所以我正在使用这个查询从我的数据库中获取一些信息。
$fetch_menu = $db->prepare("SELECT id, title, link, custom_link, rel FROM header_menu ORDER BY id DESC");
$fetch_menu->execute();
$header_menu = $fetch_menu->fetchAll();
$header_count = $fetch_menu->rowCount();
现在我正尝试使用此语句从我的数据库中回显数据。
$nav_count = 0;
while ($header_count > $nav_count){
echo "<li><a ";
if (isset($header_menu[$nav_count]['rel'])){
echo "rel='";
echo $header_menu[$nav_count]['rel'];
echo "'";
}
$nav_count ++;
}
现在,问题出现在我的网站上,我得到了这个输出(来自一个没有 rel 填充或 custom_url 填充的字段):
<a rel="" href="">Contact</a>
rel 的列在我的数据库中是空的,但它仍然回显。 我对这条语句有完全相同的问题,其中 custom_link 字段填充在菜单项的 1 个中,但其余的没有。
if (isset($header_menu[$nav_count]['custom_link'])){
echo " href='";
echo $header_menu[$nav_count]['custom_link'];
}
else if (isset($header_menu[$nav_count]['link'])){
echo " href='/";
echo $header_menu[$nav_count]['link'];
}
填充有 custom_url 的字段的输出是这样的:
<a rel="" href="http://example.com">Test page</a>
为什么我不能在我的数据库提取中使用 isset?
你可以使用 var_dump($header_menu);
查看变量中的内容。
也使用 empty() 而不是 isset()
如果您使用 mysqli,正如您用标签指明的那样,fetchAll 的工作方式类似于 mysqli_fetch_row,它将变量设置为查询返回的相应值,但 'sets NULL fields to the PHP NULL value'。所以(正如其他人在他们的评论中指出的那样)变量总是被设置,但请注意它的值可以是 NULL。
isset determines if 'a variable is set and is not NULL'. So actually, you can use isset, to determine if the query returns NULL for a field. But this is not your case, you have an empty string and you can use empty 检查一下。请注意,您不需要将 empty 与 isset 结合使用:'a variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.'
你的情况:
isset($header_menu[$nav_count]['rel']) // true, unless rel is NULL in the database
$header_menu[$nav_count]['rel'] == "" // true if rel is an empty string ("") in the database, false otherwise
empty($header_menu[$nav_count]['rel']) // true if rel is an empty string, "0" (as a string), 0, 0.0, FALSE or NULL in the database, false otherwise