我使用以下脚本来检查代码,因此当用户输入调查代码时,他们会获得与该代码相关联的调查。获取调查的部分正如其应有的那样,但我似乎无法得到错误消息,因为某些原因。如果我在这个帖子中输入错误的代码或没有代码,我得到的只是一个空白页。
<?php
$con = mysql_connect("myhost","myuser","mypassword;
if (!$con) {
die('Could not connect: ' . mysql_error());
}
// Select mysql db
mysql_select_db("mydb", $con);
$questionaireID = $_POST['questionaireID'];
$result = mysql_query("SELECT * FROM itsnb_questionaire WHERE questionaireID='$questionaireID'") or die(mysql_error());
while($row = mysql_fetch_array($result)) {
if (empty($row['questionaireID'])) {
echo '<h2>Sorry I cant find a quiz with that code, please recheck your code.</h2>';
} else {
$url = $row['questionaireurl'];
header('Location: '.$url.'');
}
}
?>
答案 0 :(得分:2)
它永远不会到达那里,因为如果结果集为空,它将跳过while循环。
尝试此操作,相反,限制为1条记录(这是您所期望的)并使用if...else
代替while
(仅在需要多个结果时才需要):
$sql = "SELECT *
FROM itsnb_questionaire
WHERE questionaireID = '{$questionaireID}'
LIMIT 1";
$result = mysql_query($sql) or die(mysql_error());
if ($row = mysql_fetch_array($result)) {
$url = $row['questionaireurl'];
header('Location: '.$url.'');
} else {
echo '<h2>Sorry I cant find a quiz with that code, please recheck your code.</h2>';
}
答案 1 :(得分:1)
如果返回的行数为零而未找到结果,则可以显示相应的错误消息
试
if (mysql_num_rows($result)<1){
//error
}