0

我创建了一个搜索框,它将从 MySQL 数据库中提取某些参数。如果没有出现参数导致回显文本通知用户没有匹配的参数可能很好,我需要将什么代码嵌入到以下代码中。

<?php
mysql_connect ("localhost", "root", "")  or die (mysql_error());
mysql_select_db ("store_location");

$term = $_POST['term'];

$sql = mysql_query("SELECT * FROM store_location where store_name like '%$term%' or     address like '%$term%' or city like '%$term%' or state like '%$term%' or zip like     '%$term%' or phone like '%$term%' or fax like '%$term%' or email like '%$term%' or url     like '%$term%' ");

echo '<h1>Search Results:</h1>';

while ($row = mysql_fetch_array($sql)){

echo 'Store Name: '.$row['store_name'];
echo '<br/> Address: '.$row['address'];
echo '<br/> City: '.$row['city'];
echo '<br/> State: '.$row['state'];
echo '<br/> Zip: '.$row['zip'];
echo '<br/> Phone: '.$row['phone'];
echo '<br/> Fax: '.$row['fax'];
echo '<br/> Email: <a href="mailto:'.$row['email'].'">'.$row['email'].'</a>';
echo '<br/> URL: <a href="'.$row['url'].'">'.$row['url'].'</a>';
echo '<br/><br/>';
}

?>
4

4 回答 4

1

要从表中获取记录数,您可以尝试以下代码:

<?php
$query = mysql_query("select count(*) as total from table_name");
$result = mysql_fetch_array($query);
echo $result['total'];
if( $result['total']==0)
{
echo "no records is found";
}else{
echo"numbers of records is=".$result['total'];
}

?>
于 2012-12-06T16:49:11.997 回答
1

在该while...行之前添加:

if( mysql_num_rows($sql) == 0) echo "<p>No matches</p>";

文档

于 2012-12-06T16:25:52.193 回答
0
<?

$term = mysql_real_escape_string($_POST['term']); // sanitize input!
if ($sql = mysql_query('...')) { // mysql_* functions are deprecated, consider mysqli instead
    $count = mysql_num_rows($sql);
    echo "<h1>$count Result" . ($count == 1 ? '' : 's') . ' Found' . ($count ? ':' : '') . '</h1>';
    while ($row = mysql_fetch_array($sql)) {
        echo '...';
    }
}
于 2012-12-24T19:33:13.473 回答
0

尝试这个。mysql_num_rows

if(mysql_num_rows($sql)){
    while ($row = mysql_fetch_array($sql)){
        echo 'Store Name: '.$row['store_name'];
        echo '<br/> Address: '.$row['address'];
        echo '<br/> City: '.$row['city'];
        echo '<br/> State: '.$row['state'];
        echo '<br/> Zip: '.$row['zip'];
        echo '<br/> Phone: '.$row['phone'];
        echo '<br/> Fax: '.$row['fax'];
        echo '<br/> Email: <a href="mailto:'.$row['email'].'">'.$row['email'].'</a>';
        echo '<br/> URL: <a href="'.$row['url'].'">'.$row['url'].'</a>';
        echo '<br/><br/>';
    }
}else{
    echo 'No results found';
}
于 2012-12-06T16:26:42.740 回答