【问题标题】:PHP Search Not Working When I Get To The While Loop当我进入 While 循环时,PHP 搜索不起作用
【发布时间】:2015-09-30 13:34:08
【问题描述】:

我的网站有一个搜索表单,效果非常好,但后来我更改了整个登录系统,这导致我的网站出现了一些错误。现在我似乎无法修复的唯一错误是我网站中的搜索表单。我认为这很奇怪,因为表单不使用登录表单中的任何变量。当我在测试以找出问题时,我发现当我的搜索脚本的一部分进入 while 循环时,它会在我身上中断。如果您有任何建议或帮助,将不胜感激。我将在下面提供我的代码。

hub.php

 <?php

  // this file connects to the database
  include("includes/connect.inc.php");

  session_start();

  if (isset($_SESSION['id'])) {
    $uid        = $_SESSION['id'];
  } 

//If the search input was submitted then run
if(isset($_POST['search'])){
    // turn that the user searched into a varible
    $searchQ = $_POST['search'];
    // delete any symbols for security
    $searchQ = preg_replace("#[^0-9a-z]#i", "", $searchQ);
    // Define varibles before the loop
    $searchArray = array();
    $searchIndex = 0;

    // Search through these columns inside the main database
    $searchQuery = mysqli_query("SELECT * FROM database WHERE 
        title   LIKE '%" . mysqli_escape_string( $searchQ ) . "%' 
    ");

    // count the number of results
    $searchCount = mysqli_num_rows($searchQuery);
    if($searchCount != 0){
        while($row = mysqli_fetch_array($searchQuery)){
            $titleSearch     = $row['title'];
            $dateSearch      = $row['date'];

            // buile the array which will hold all the results
            $searchArray[$searchIndex] = array($titleSearch, $dateSearch);
            $searchIndex++;
        }
    }
}
// End of search php



<form id="search" action="hub.php" method="POST">
    <div id="searchIcon"></div>
    <input type="search" name="search" placeholder="Search">
</form>

这里是连接文件 |它用于一堆其他工作正常的形式和循环。

<?php

    $host = "MYHOSTINGDOMAIN";
    $username = "MYUSERNAME";
    $password = "MYPASSWORD";
    $db = $username;

    $connect = mysqli_connect($host, $username, $password, $db) or die(mysqli_connect_error());

我收到此错误

警告:mysqli_query() 至少需要 2 个参数,其中 1 个在 /services/webpages/d/i/digitalicon.ca/public/hub.php 在第 42 行

警告:mysqli_num_rows() 期望参数 1 为 mysqli_result, /services/webpages/d/i/digitalicon.ca/public/hub.php 中给出的 null 第 45 行

【问题讨论】:

  • 错误是什么?你调试了吗?\

标签: php mysql forms loops search


【解决方案1】:

好的,所以根据提供的更新信息,看起来您是通过 mysqli_* 函数进行连接,然后在此脚本中使用 mysql_* 函数(切换到 mysqli 可能是您登录更改的一部分)。

所以你需要更新你的脚本变成这样:

<?php

// this file connects to the database
include("includes/connect.inc.php");

session_start();

if (isset($_SESSION['id'])) {
    $uid        = $_SESSION['id'];
} 

// //If the search input was submitted then run
if(isset($_POST['search'])){
    // turn that the user searched into a varible
    $searchQ = $_POST['search'];
    // delete any symbols for security
    $searchQ = preg_replace("#[^0-9a-z]#i", "", $searchQ);
    // Define varibles before the loop
    $searchArray = array();
    $searchIndex = 0;

    // Search through these columns inside the main database
    $searchQuery = mysqli_query("SELECT * FROM database WHERE 
        title   LIKE '%" . mysqli_escape_string( $searchQ ) . "%' or 
        date    LIKE '%" . mysqli_escape_string( $searchQ ) . "%' 
    ");

    // count the number of results
    $searchCount = mysqli_num_rows($searchQuery);
    if($searchCount != 0){
        while($row = mysqli_fetch_array($searchQuery)){
            $titleSearch     = $row['title'];
            $dateSearch      = $row['date'];

            // buile the array which will hold all the results
            $searchArray[$searchIndex] = array($titleSearch, $dateSearch);
            $searchIndex++;
        }
    }
}
// End of search php

这包括我最初的修复,即实际查询您之前只是转义字符串的数据库。

【讨论】:

  • 我只是将其更改为您提供的示例,但似乎没有任何改变。我确实注意到,当我更改它时,我的文本编辑器中的颜色标识符在查询中的第一个“或”之后将 mysql 字体从粉红色更改为黄色(好像它只是一个字符串而不是 mysql 字符串)。跨度>
  • 运行代码时遇到什么错误?
  • 根据您发布的错误,您的脚本实际上并未连接到数据库。您能否将include("includes/connect.inc.php"); 中包含的代码包含在内,以便我们查看您的连接代码? (确保编辑连接的用户名/密码)
  • 刚刚添加了连接文件,但我不明白为什么它会引起我的问​​题,因为我也将它用于我网站中的许多其他事情。会话是否可能导致某种问题,因为在我的所有会话正常工作之前它工作正常。
  • 我认为问题在于您通过 mysqli_* 进行连接,但这个特定脚本仍在使用 mysql_* 函数。我已更新我的答案以反映您需要的更改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2011-10-19
相关资源
最近更新 更多