gpt4 book ai didi

php - 执行语句后空白页

转载 作者:行者123 更新时间:2023-12-01 00:04:41 25 4
gpt4 key购买 nike

我编写了一个从数据库中输出动物名称的程序。但是我遇到了一个问题,即当我的程序找不到正确的值(不在数据库中的值)时,我的程序无法执行该案例

<?php
$host='localhost';
$user='root';
$password='root';
$dbname='pet';
$connect=mysqli_connect($host,$user,$password,$dbname) or die("can not connect to server");

if(@$_GET['data']=='yes')
{
$animalName=trim($_POST['animal']);
$query="SELECT name FROM display WHERE name='$animalName'";
$result=mysqli_query($connect,$query) or die("can not execute query");
while($row=mysqli_fetch_assoc($result))
{
extract($row);
if(!$name==$animalName)
{
echo "not found";
}
else
{
echo "Hello $name. Have a nice day.";
}
}

}
else
{
echo "<form action='$_SERVER[PHP_SELF]?data=yes' method='POST'>
<h4>type an animal name in the box below and press enter</h4>
<p><input type='text' name='animal' maxlength='20'></p>
<p><input type='submit' value='submit'></p>
</form>";
}
?>

当我键入数据库中不存在的动物时,我的代码无法执行此 block (它变成一个空白页)

if(!$name==$animalName)
{
echo "not found";
}

有什么解决办法吗

最佳答案

如果您没有从数据库中获取结果集中的任何行,则 while 循环内的代码将永远不会执行。

您可能需要做的是在尝试进入 while 循环之前检查结果集中的行数。如果您有 0 行,只需发出消息,不要尝试在 while 循环中从数据库中“获取”任何结果。

关于php - 执行语句后空白页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19818016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com