我是编程的初学者。我正在学习PHP。我想要做的是用PHP为我的项目编写一个搜索脚本。当我尝试搜索MySQL数据库时,它给了我一个错误:
注意:未定义的变量:第2行的C:\ xampp \ htdocs \ search.php中的输出
我检查了脚本上的所有内容,但无法查看问题。我编错了吗?
我已经在论坛上检查了与我的问题相关的所有可能的问题,他们似乎没有给我提供我需要的答案。请帮忙。
这是带有输入的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");?>