我希望脚本在没有找到数据库条目时给出错误。此脚本适用于$ _GET函数。例如,当时。用户输入:index.php?id = 1054355 它找不到任何东西。它会输出一条未找到条目的消息。
我现在的代码:
$userid = mysql_real_escape_string($_GET['id']);
$query = mysql_query("SELECT * FROM nieuws WHERE UserID = '$userid'") or die(mysql_error());
//fetch the results / convert results into an array
while($rows = mysql_fetch_array($query)) {
$userid = $rows['UserID'];
$titel = $rows['Titel'];
$body = $rows['Body'];
$door = $rows['Door'];
$bron = $rows['Bron'];
echo
"
<h1><b>Nieuws - $titel</b></h1>
<p> </P>
<h4>$body</h4>
<p> </p>
<p> </p>
<b>Geschreven door: $door</b><p>
<i>Bronvermelding: $bron</i>
";
}
?>
我希望somone可以提出解决方案,
欢呼声。
答案 0 :(得分:0)
使用numrows http://tr1.php.net/mysql_num_rows
你必须注意到mysql将很快不再可用。
改用mysqli。
$numRows = mysql_num_rows($query);
if($numRows == 0){
echo "No record found!";
}
答案 1 :(得分:0)
你可以这样做:
$userid = mysql_real_escape_string($_GET['id']);
$query = mysql_query("SELECT * FROM nieuws WHERE UserID = '$userid'") or die(mysql_error());
if (mysql_num_rows($result) == 0) {
echo 'No rows found';
} else {
//fetch the results / convert results into an array
while($rows = mysql_fetch_array($query)) {
$userid = $rows['UserID'];
// etc...
}
}
注意: mysql_*()
函数已弃用,很快就会删除。使用mysqli_*()
或PDO
获取新代码。
答案 2 :(得分:0)
$userid = mysql_real_escape_string($_GET['id']);
$query = mysql_query("SELECT * FROM nieuws WHERE UserID = '$userid'") or die(mysql_error());
if(mysql_fetch_array($query ) == false)
return 'no data found';
//fetch the results / convert results into an array
while($rows = mysql_fetch_array($query)) {
$userid = $rows['UserID'];
$titel = $rows['Titel'];
$body = $rows['Body'];
$door = $rows['Door'];
$bron = $rows['Bron'];
echo
"
<h1><b>Nieuws - $titel</b></h1>
<p> </P>
<h4>$body</h4>
<p> </p>
<p> </p>
<b>Geschreven door: $door</b><p>
<i>Bronvermelding: $bron</i>
";
}
?>
另一种方式:
if(mysql_fetch_row($query ))
return "no data found!";
else{
// do your stuff
}
在mysqli中:
if($query->num_rows)
return "no data found!";
else{
// do your stuff
}
答案 3 :(得分:0)
你可以这样做:
$result = mysql_query($sql,$con);
$row_count = mysql_num_rows($result);
if($row_count === 0)
{
echo "nothing found" ;
}
else
{
// your code for the retrieved result
}
答案 4 :(得分:0)
if(!$query){ //will return true if query was not successful
echo 'id could not be found';
}else{
while($rows = mysql_fetch_array($query)) {
$userid = $rows['UserID'];
$titel = $rows['Titel'];
$body = $rows['Body'];
$door = $rows['Door'];
$bron = $rows['Bron'];
echo
"
<h1><b>Nieuws - $titel</b></h1>
<p> </P>
<h4>$body</h4>
<p> </p>
<p> </p>
<b>Geschreven door: $door</b><p>
<i>Bronvermelding: $bron</i>
";
}
}
答案 5 :(得分:0)
使用mySQLi(应该使用),
$write = new mysqli($db_hostname,$db_username,$db_password,$db_database); //use your values here
$stmt = $write->prepare("SELECT `UserID`,`Titel`,`Body`,`Door`,`Bron` WHERE `userID` = ?");
$stmt->bind_param('i',$userId); // i = int, s = string.
$stmt->bind_result($userid,$titel,$body,$door,$bron);
$stmt->fetch();
$stmt->execute();
$stmt->close();
这只是一个基本的例子,你可以做很多事情来检查结果,一个这样的方法就是,
if(isset($titel)){//code here to do stuff}
可能值得做的事情是如果没有找到结果,则将用户重定向到通用页面,
if(!isset($titel){header("location:index.php");} //This checks if titel is set, if not then...
正如Awlad Linton所说,你也可以mysqli_num_rows()
,这可能更有效率。