数据库中的简单 PHP 搜索不会显示结果
Simple PHP search in Database won't bring up result
我正在和我的一些朋友建立一个食谱数据库,为此我们需要我们的用户能够在我们的网站内进行搜索。我们的数据库包含 3 个表:
食谱 - recipe_id(主键),recipe_name
成分-ingredient_id(主键),ingredient_name
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 中的搜索函数中,无论我们搜索什么,它每次都会返回 'There were no search results!'。这是代码。有人会指出我们犯的错误吗?
$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>';
}
}
}
我们不明白为什么它不起作用。
首先 - 这取决于您使用的 PHP 版本,正如 Jens 指出的那样,mysql_* 已被弃用。
其次 - 您似乎没有连接到数据库。您必须先连接到数据库,然后执行查询。
查看 PHP's website 上的这个示例,它应该对您有很大帮助。
祝你好运!
您似乎一直在搜索 "searchq",您可能没有那个名字的食谱,我建议您不要使用 mysql_* funcs
$输出='';
如果(isset($_POST['work'])){
$searchq = $_POST['work'];
$searchq = preg_replace("#[^0-9a-z]#
我","",$searchq);
$查询= 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
其中 ingredient_name 喜欢 '%
$searchq%' 或 recipe_name LIKE '%
$searchq%'")
您可以尝试以下方法。它使用 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>';
}
}
}
我正在和我的一些朋友建立一个食谱数据库,为此我们需要我们的用户能够在我们的网站内进行搜索。我们的数据库包含 3 个表:
食谱 - recipe_id(主键),recipe_name
成分-ingredient_id(主键),ingredient_name
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 中的搜索函数中,无论我们搜索什么,它每次都会返回 'There were no search results!'。这是代码。有人会指出我们犯的错误吗?
$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>';
}
}
}
我们不明白为什么它不起作用。
首先 - 这取决于您使用的 PHP 版本,正如 Jens 指出的那样,mysql_* 已被弃用。
其次 - 您似乎没有连接到数据库。您必须先连接到数据库,然后执行查询。
查看 PHP's website 上的这个示例,它应该对您有很大帮助。
祝你好运!
您似乎一直在搜索 "searchq",您可能没有那个名字的食谱,我建议您不要使用 mysql_* funcs $输出=''; 如果(isset($_POST['work'])){ $searchq = $_POST['work']; $searchq = preg_replace("#[^0-9a-z]# 我","",$searchq); $查询= 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 其中 ingredient_name 喜欢 '% $searchq%' 或 recipe_name LIKE '% $searchq%'")
您可以尝试以下方法。它使用 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>';
}
}
}