我是编程初学者。我正在学习PHP。我想要做的是在PHP中为我的项目编写一个搜索脚本。当我尝试通过MySQL数据库搜索它给我一个错误:PHP搜索代码不输出结果结果
Notice: Undefined variable: output in C:\xampp\htdocs\search.php on line 2
我已经检查了脚本上的一切,不能看到问题。我编码错了吗?
我已经在论坛上检查了所有可能的问题,涉及到我的问题,他们似乎没有给我留下我需要的答案。请帮忙。
这是HTML脚本的输入:
<form action="search.php" method="post" id="search">
<div id="searchfield_div">
<input name="search" id="searchfield" type="text" placeholder="What you looking for?">
<input id="delete_search_button" name="delete button" type="button" value="X">
<input id="search_button" name="search button" type="submit" value="Search">
</div>
</form>
,这是我的PHP脚本:
<?php
//connection script
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "liquihub";
$output = '';
@mysqli_connect("$db_host","$db_user","$db_pass","$db_name") or die("could not connect");
//collection script
if (isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","", $searchq);
$query = mysql_query("SELECT * FROM beverage_db WHERE name LIKE '%$searchq%' OR price LIKE '%$searchq%' OR type LIKE '%$searchq%'") or die ("could not search");
$count = mysql_num_rows($query);
if ($count == 0) {
$output = 'We not stocking this particular item at present';
}else{
while($row = mysql_fetch_array($query)) {
$bevname = $row['name'];
$bevprice = $row['price'];
$bevtype = $row['type'];
$bevid = $row['id'];
$output .= '<div>'.$bevname.' '.$bevprice.' '.$bevtype.'</div>';
}
}
}
?>
说的意思把结果在不同的页面输出脚本:
<?php print("$output");?>
你不能混用'mysqli_'和'mysql_'功能。你应该坚持'mysqli_' – tkausl
我已经将它们全部更改为mysqli_,但它仍然是相同的问题 – user2938948
可能重复的[PHP:“注意:未定义的变量”和“注意:未定义的索引”](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – matiaslauriti