如果值没有找到mysql显示错误消息 [英] Display error message if value not found mysql
问题描述
您好,我已经做了关于这个主题的研究,我确实遇到了几个解决方案,虽然我不能实现他们到我的代码,因为我是一个初学者。我的问题是基本上如何显示一条消息,如果值没有在MySQL数据库中找到?
Hi i have done research on this topic i did come across a few solutions, although i wasn't able to implement them to my code because i am a beginner to this. My question is basically how can i display a message if value is not found in the MySQL database?
先前搜索过:在PHP MySQL搜索中找不到结果时显示消息和 mysql fetch array
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>
<?php
$customer = $_GET["custID"];
$conn = mysql_connect("localhost", "localuser", "pass");
mysql_select_db("testdb", $conn)
or die ('Database not found ' . mysql_error() );
$sql = "SELECT orderNumber, customerID, orderDate, shippingDate, shipped FROM orders where customerID = $customer ORDER by orderDate";
$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());
?>
<table border="1" summary="Customer Details">
<tr>
<th>Order Number</th>
<th>Customer ID</th>
<th>Order Date</th>
<th>Shipping Date</th>
<th>Shipped</th>
</tr>
<?php
$results = mysql_fetch_array($rs);
if ( $results === FALSE )
{
echo "No result";
}
else
{
foreach($results as $item)
{?>
<tr>
<td><?php echo $result["orderNumber"]?></td>
<td><?php echo $result["customerID"]?></td>
<td><?php echo $result["orderDate"]?></td>
<td><?php echo $result["shippingDate"]?></td>
<td><?php echo $result["shipped"]?></td>
</tr>
<?php }
}
mysql_close($conn); ?>
</table>
</body>
</html>
推荐答案
此用法 if ($ result> 0)) 错误。当没有结果时, mysql_fetch_row 始终返回FALSE。请参阅: http://php.net/manual/en/function。 mysql-fetch-array.php
This usage if(count($result > 0)) is wrong. mysql_fetch_row always returns FALSE when there is no result. Please see: http://php.net/manual/en/function.mysql-fetch-array.php
您应该使用类似的
<?php
$results = mysql_fetch_array($rs);
if ( $results === FALSE )
{
echo "No result";
}
else
{
foreach($results as $item)
{
...
}
}
?>
这篇关于如果值没有找到mysql显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!