在数据库中进行简单的PHP搜索不会产生结果

时间:2015-10-08 13:20:40

标签: php mysql search chef-recipe

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

食谱 - recipe_id 主键), recipe_name

成分 - ingredient_id 主键),成分名称

recipe_ingredients - ingredient_id 外键), recipe_id 外键

我们希望能够在recipe_ingredients中搜索配方或配料名称,并让我们的网站显示与该配方相关的每种成分或与该成分相关的每种配方。所以我们提出了这个问题:

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中的搜索功能中,它会给出“没有搜索结果!”#39;无论我们搜索什么,每一次都会回来。这是代码。有人会指出我们犯的错误吗?

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

我们不明白为什么它不会工作。

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

首先 - 它取决于您使用的PHP版本,因为Jens指出mysql_ *已被弃用。

其次 - 您似乎没有连接到数据库。您必须先连接到数据库,然后执行查询。

PHP's website上查看此示例,它可以为您提供很多帮助。

祝你好运!

答案 2 :(得分:0)

好像你一直在寻找“searchq”,你可能没有这个名字的配方,我会建议agajnst mysql_ * funcs      $ output ='';      if(isset($ _ POST ['work'])){     $ searchq = $ _POST ['work'];      $ searchq = preg_replace(“#[^ 0-9a-z]#    我”, “”,$ SEARCHQ);       $ query = mysql_query      (“select ingredient_name,recipe_name,recipe_ingredients。*     来自recipe_ingredients      内连接成分内连接配方     在recipe_ingredients.ingredient_id = ingredients.ingredient_id       和recipe_ingredients.recipe_id = recipes.recipe_id       在哪里成份名称喜欢'%      $ searchq%'或recipe_name LIKE'%      $ SEARCHQ%'“)