您好我已经对这个主题进行了研究我遇到了一些解决方案,虽然我无法将它们实现到我的代码中,因为我是初学者。我的问题基本上是如果在MySQL数据库中找不到值,我该如何显示消息?
之前已搜索过:Displaying message when no results found in PHP MySQL search和mysql fetch array if no results display message
<?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>
答案 0 :(得分:0)
使用mysql_num_rows()
if(mysql_num_rows($rs) > 0) {
// got records
}
else {
// no records found
}
注意:
不要使用mysql_ *系列函数,因为它们将被弃用。开始研究mysqli或pdo
答案 1 :(得分:0)
此用法 if(count($ result&gt; 0)) 错误。当没有结果时, mysql_fetch_row 总是返回FALSE。请参阅: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)
{
...
}
}
?>
答案 2 :(得分:0)
$result = mysql_fetch_array($rs)
if($result)
...