我想添加文字'没有记录,请再试一次'来搜索没有任何内容的搜索。请注意,我是初学者,我的代码如下:
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM search WHERE surname LIKE '%".$term."%' ORDER BY surname, fullname, conflictsort";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)){
$id = $row['id'];
$initial = $row['initial'];
$surname = $row['surname'];
$fullname = $row['fullname'];
$dob = $row['dob'];
$born = $row['born'];
$service = $row['service'];
$enlisteddate = $row['enlisteddate'];
$enlisted = $row['enlisted'];
$number = $row['number'];
$rank = $row['rank'];
$batt = $row['batt'];
$unit = $row['unit'];
$section = $row['section'];
$died = $row['died'];
$death = $row['death'];
$memorial = $row['memorial'];
$conflict = $row['conflict'];
$conflictsort = $row['conflictsort'];
$biography = $row['biography'];
$link = $row['link'];
echo"<h4>$surname</h4>
<b>Name:</b> $fullname - <b>Rank:</b> $rank ($number) - <b>Regiment:</b> $batt $unit $section
<br /><b>Birth:</b> $dob $born
<br /><b>Enlisted:</b> $service - $enlisteddate $enlisted
<br /><b>Died:</b> $died $death
<br /><b>Memorial:</b> Shown as $initial$surname at $memorial
<br /><b>Confilct:</b> $conflict
<br /><b><a href='$link'>$biography</a></b>
<br /><br /><br />";
}
}
&GT;
答案 0 :(得分:2)
使用mysql_num_rows()
函数检查SELECT
查询返回的行数,并相应地显示结果。
// your code
$r_query = mysql_query($sql);
if(mysql_num_rows($r_query)){
while ($row = mysql_fetch_array($r_query)){
// your code
}
}else{
echo "no records, please try again";
}
Sidenote :不要使用mysql_*
函数,从PHP 5.5开始不推荐使用它们,并在PHP 7.0中完全删除它们。请改用mysqli
或pdo
。 And this is why you shouldn't use mysql_*
functions。