搜索功能就像一个魅力,但我不知道如何添加某些“额外”。当有结果显示时,我也想说:
"Your search returned x results."
随后是结果。
如果没有显示结果,我想说:
"Your search returned no results. Please try again."
当用户没有在搜索表单中输入任何内容时,我想说:
"You did not enter a search term."
我是PHP初学者,我不知道如何将其实现到我当前的代码中;我尝试了很多不同的方法,它或者给我错误,或者在没有结果时返回空白页。
任何方向或帮助都会很棒。谢谢。
这是我目前的代码:
<?php
//STEP 1 Connect To Database
$connect = mysql_connect("localhost","tarb89_admin","leccums");
if (!$connect)
{
die("MySQL could not connect!");
}
$DB = mysql_select_db('tarb89_characters');
if(!$DB)
{
die("MySQL could not select Database!");
}
//STEP 2 Check Valid Information
if(isset($_GET['search']))
{
//STEP 3 Declair Variables
$Search = $_GET['search'];
$result = mysql_query("SELECT * FROM characters WHERE name LIKE '%$Search%' ");
while($row = mysql_fetch_assoc($result))
{
$name = $row['name'];
$id = $row['id'];
$breed = $row['breed'];
$gender = $row['gender'];
$genetics = $row['genetics'];
$profile = $row['profile'];
$player = $row['player'];
$color = $row['color'];
$markings = $row['markings'];
$traits = $row['traits'];
$defects = $row['defects'];
$extras = $row['extras'];
echo " <h3>$name</h3>
<table width='700px' cellpadding='5' cellspacing='0'>
<tr>
<td><p>
<b>ID Number: </b>$id<br>
<b>Breed: </b>$breed<br>
<b>Gender: </b>$gender<br>
<b>Genetics: </b>$genetics<br>
<b>Profile:</b> <a href='$profile'>$profile</a><br>
<b>Player:</b> $player</p></td>
<td><p>
<b>Color:</b> $color<br>
<b>Markings:</b> $markings<br>
<b>Traits:</b> $traits<br>
<b>Defects:</b> $defects<br>
<b>Extras:</b> $extras</p></td>
</table>";
}
}
?>
答案 0 :(得分:1)
使用mysql_num_rows()
返回行数
if($search!="") {
// Your Query
$num = mysql_num_rows($result);
if($num >= 1)
{
echo "Your search returned $num results";
// your code
}
else
{
echo "Your search returned no results. Please try again";
}
} else {
echo "You did not enter a search term";
}
您的代码
if(isset($_GET['search']))
{
//STEP 3 Declair Variables
$Search = $_GET['search'];
$result = mysql_query("SELECT * FROM characters WHERE name LIKE '%$Search%' ");
$num = mysql_num_rows($result);
if($num >= 1)
{
echo "Your search returned $num results";
while($row = mysql_fetch_assoc($result))
{
$name = $row['name'];
$id = $row['id'];
$breed = $row['breed'];
$gender = $row['gender'];
$genetics = $row['genetics'];
$profile = $row['profile'];
$player = $row['player'];
$color = $row['color'];
$markings = $row['markings'];
$traits = $row['traits'];
$defects = $row['defects'];
$extras = $row['extras'];
echo " <h3>$name</h3>
<table width='700px' cellpadding='5' cellspacing='0'>
<tr>
<td><p>
<b>ID Number: </b>$id<br>
<b>Breed: </b>$breed<br>
<b>Gender: </b>$gender<br>
<b>Genetics: </b>$genetics<br>
<b>Profile:</b> <a href='$profile'>$profile</a><br>
<b>Player:</b> $player</p></td>
<td><p>
<b>Color:</b> $color<br>
<b>Markings:</b> $markings<br>
<b>Traits:</b> $traits<br>
<b>Defects:</b> $defects<br>
<b>Extras:</b> $extras</p></td>
</table>";
}
}
else
{
echo "Your search returned no results. Please try again";
}
} else {
echo "You did not enter a search term";
}
注意: mysql_*
函数已弃用,请{as}移动mysqli_*
函数