HTML搜索字段总是回归零结果

时间:2015-04-12 18:37:00

标签: php html5 search-engine

所以基本上我认为在我的网站上添加一个搜索字段不在我的联盟中,但互联网有太多的信息我决定试一试。

我首先在我的索引pagina中添加了一个表单,这是我正在使用的代码:

        <form method="get" action="search.php"> 
            <table cellpadding="0px" cellspacing="0px"> 
              <tr> 
              <td style="border-style:solid none solid solid;border-color:#4B7B9F;border-width:1px;">
                <input type="text" name="q" style="width:100px; border:0px solid; height:17px; padding:0px 3px; position:relative;"> 
              </td>
              <td style="border-style:solid;border-color:#4B7B9F;border-width:1px;"> 
                <input type="submit" value="" style="border-style: none; background: url('images/searchicon.gif') no-repeat; width: 24px; height: 19px;">
              </td>
              </tr>
            </table>
        </form>

接下来,我的search.php代码:

<?php
    ini_set('display_errors', 0);
    $search = $_GET ['q'];
    mysql_connect("localhost", "root", "");
    mysql_select_db("release");
    $query    = mysql_query("SELECT * FROM game WHERE name LIKE '%" . $queryString . "%'");
    $foundnum = mysql_num_rows($query);

     if ($foundnum == 0) {
       echo "No results found. Either this game doesn't exist, or we have yet to add it. Please contact us!";
     }
     else {
       echo "$foundnum results found !<p>";
       $row = mysql_fetch_assoc($query);
     {
       echo '<p>'.$row['game_name'].'</p>';
     }
     }
?>

即使我搜索的数据在游戏桌中,查询也会不断回显$foundnum == 0消息。

然而,当我尝试这段代码时:

$query    = mysql_query("SELECT game_name FROM game WHERE game_name LIKE '%" . $queryString . "%'");

查询在我的屏幕上打印'找到'35个结果'。我在数据库中有35个条目,但这对我来说没有意义,因为我正在搜索一个只输入一次的游戏名称......

2 个答案:

答案 0 :(得分:1)

首先,您使用的是不推荐使用的版本mysql_ *需要使用mysqli_ *。请查看以下内容。

    <?php
ini_set('display_errors', 2);
$search = $_GET ['q'];
// see changes from below line
$conn = mysqli_connect("localhost", "root", "","release");
$query    = mysqli_query($conn,"SELECT game_name FROM game WHERE game_name LIKE '%". $search ."%'");
$foundnum = mysqli_fetch_array($query);
$count = count($foundnum['game_name']);
 if ($foundnum == 0) {
   echo "No results found. Either this game doesn't exist, or we have yet to add it. Please contact us!";
 }
 else {
   echo "$count results found !<p>";
   echo"<pre/>";print_r($foundnum['game_name']);
 }
?>

注意: - 从您在上面写的索引和搜索文件中删除包含代码。

答案 1 :(得分:0)

从你的第二个代码看,似乎有一个错字。

尝试将$ query更改为

$query    = mysql_query("SELECT * FROM game WHERE game_name LIKE '%" . $queryString . "%'");

让我知道它是否有效:)