HTML 搜索字段总是回显零个结果

HTML search field always echoeing zero results

所以基本上我认为在我的网站上添加一个搜索字段超出了我的能力范围,但是互联网上有太多的信息所以我决定试一试。

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

        <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>';
     }
     }
?>

尽管我搜索的数据在游戏中 table.

,但查询不断回显 $foundnum == 0 消息

但是,当我尝试此代码时:

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

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

从你的第二个代码来看,好像有错字。

尝试将 $query 更改为

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

让我知道它是否有效:)

首先你使用的是弃用版本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']);
 }
?>

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