3

搜索功能就像一个魅力,但我不知道如何添加某些“额外”。当有结果要显示时,我还想说:

"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>";

    }
}
?>
4

1 回答 1

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_*功能已弃用,请mysqli_*尽快使用功能

于 2013-11-07T16:37:38.743 回答