php - 数据库中的简单 PHP 搜索不会显示结果

标签 php mysql search chef-recipe

我正在与我的一些 friend 建立一个 Recipe 数据库,为此我们需要我们的用户能够在我们的网站内进行搜索。我们的数据库由 3 个表组成:

Recipe - recipe_id(主键),recipe_name

成分 - ingredient_id(主键),ingredient_name

recipe_ingredients - ingredient_id(外键),recipe_id(外键)

我们希望能够在 recipe_ingredients 中搜索 Recipe 或配料名称,并让我们的网站显示与该 Recipe 相关的每种配料或与该配料相关的每种 Recipe 。所以我们做了这个查询:

select ingredient_name, recipe_name, recipe_ingredients.*
from recipe_ingredients
inner join ingredients inner join recipes
on recipe_ingredients.ingredient_id = ingredients.ingredient_id
and recipe_ingredients.recipe_id = recipes.recipe_id

WHERE ingredient_name = 'Brød';

这对我们来说很好。然而,将它放入我们的 php 搜索功能中,它会给出“没有搜索结果!”无论我们搜索什么,每次都会返回。这是代码。有人会指出我们犯的错误吗?

$output = '';
if (isset($_POST['work'])) {
 $searchq = $_POST['work'];
 $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
 $query = mysql_query 

  ("select ingredient_name, recipe_name, recipe_ingredients.*
  from recipe_ingredients
  inner join ingredients inner join recipes
  on recipe_ingredients.ingredient_id = ingredients.ingredient_id
  and recipe_ingredients.recipe_id = recipes.recipe_id
  WHERE ingredient_name LIKE '%searchq%' or recipe_name LIKE '%searchq%'")

 or die ("Could not search");
 $count = mysql_num_rows($query);
 if($count == 0){
    $output = 'There were no search results!';
 }
 else{
  while ($row = mysql_fetch_array($query)) {
   $recipe = $row[recipe_name];
   $ingredient = $row[ingredient_name];
   $id = $row[ingredient_id];

   $output .= '<div>'.$recipe.' '.$ingredient.'</div>';
  }
 }
}

我们不明白为什么它不起作用。

最佳答案

您可以尝试以下操作。使用 mysql_* 函数和更好的查询连接结构。

$connection = mysqli_connect('localhost', 'root', 'your_password', 'your_database');
mysqli_set_charset($connection, 'utf8');
if (!$connection) {
    die("Database connection failed: " . mysqli_error());
}

$output = '';
if (isset($_POST['work'])) {

    $searchq = $_POST['work'];
    $searchq = preg_replace("#[^0-9a-z]#i", "", $searchq);

    $sql = "
        SELECT ingredient_name, recipe_name, recipe_ingredients.*
        FROM recipe_ingredients
        INNER JOIN ingredients
        ON recipe_ingredients.ingredient_id = ingredients.ingredient_id
        INNER JOIN recipes
        ON recipe_ingredients.recipe_id = recipes.recipe_id
        WHERE ingredient_name LIKE '%$searchq%' or recipe_name LIKE '%$searchq%'";

    $result = mysqli_query($connection, $sql);
    if (!$result) {
        die("SQL Error: " . mysqli_error($connection);
    }

     $count = mysqli_num_rows($result);

     if ($count == 0) {
        $output = 'There were no search results!';
     } else {
        while ($row = mysqli_fetch_array($result)) {
            $recipe = $row[recipe_name];
            $ingredient = $row[ingredient_name];
            $id = $row[ingredient_id];

            $output .= '<div>'.$recipe.' '.$ingredient.'</div>';
        }
    }
}

关于php - 数据库中的简单 PHP 搜索不会显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33017091/

相关文章:

string - 使用 InStr() vba 在字符串中搜索 3"

c# - 字符串是否存在检查 20k 次

algorithm - 二叉树挑战示例

php - 框架问题

php - 每次 while 循环运行时调用 javascript 以提供独特的波形

mysql - Hibernate:ORDER BY 子句中的 OR

mysql - SQL查询无法处理引号

php - mysql wordpress php 语法错误

php - 为什么 Heroku 服务器上的 Laravel JWT auth 出现此错误

php - 如何在 PHP 中使用 RS256 签署 X.509 证书?无法获取有效指纹...x5t