我正在搜索框,我遇到了一个小问题。我已经阅读了一些与我相同的问题,并尝试了那里提供的解决方案,但它们并没有奏效。我需要回显我的SELECT查询的结果。
<?Php
$cat_id_in = (int) $_GET['cat_id'];
$cat_dropdown = Category::getCat_DropDown($cat_id_in);
if(isset($_POST['save'])) {
$search = $_POST['search'];
//connect to database
$mysqli = mysqli_connect("localhost", "php24sql", "hJQV8RTe5t", "smithside") or die('Could not connect: ' . mysql_error());
$query2 = "SELECT * FROM lots WHERE lot_name OR lot_description LIKE '%$search%' AND cat_id = '$cat_id_in'";
$results = mysql_query($query2);
while ($row = mysql_fetch_assoc($results)) {
echo $row['lot_name'] . "<br />";
}
}
?>
错误是:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\System_CompA2\content\search.php on line 12.
我的查询肯定有效,因为我用if语句试了一下,并且我的数据库连接没有错误。那有什么可能是错的?非常感谢!
答案 0 :(得分:1)
你正在混合mysql和mysqli函数,你在sql查询中也有错误
试试这个:
$mysqli = mysqli_connect("localhost", "php24sql", "hJQV8RTe5t", "smithside") or die('Could not connect: ' . mysql_error());
$query2 = "SELECT * FROM lots WHERE (lot_name LIKE '%$search%' OR lot_description LIKE '%$search%') AND cat_id = '$cat_id_in'";
$results = mysqli_query($mysqli,$query2);
while ($row = mysqli_fetch_assoc($results)) {
echo $row['lot_name'] . "<br />";
}
有关mysqli_*
功能的更多信息,请阅读 this
答案 1 :(得分:1)
不要混用mysql和mysqli ....
在mysqli中查询时使用连接
//connect to database
$mysqli = mysqli_connect("localhost", "php24sql", "hJQV8RTe5t", "smithside");
if (!$mysqli) {
die("Connection failed: " . mysqli_connect_error());
}
$query2 = "SELECT * FROM lots WHERE lot_name LIKE '%$search%' OR lot_description LIKE '%$search%' AND cat_id = '$cat_id_in'";
$results = mysqli_query($mysqli ,$query2);
while ($row = mysqli_fetch_assoc($results)) {
echo $row['lot_name'] . "<br />";
}
答案 2 :(得分:0)
WHERE中有错误。
WHERE lot_name OR lot_description LIKE '%$search%'
应该是
WHERE lot_name LIKE '%$search%' OR lot_description LIKE '%$search%'"
答案 3 :(得分:0)
您正在使用&#34; mysql_fetch_assoc&#34;和&#34; mysql_query&#34;当连接是通过&#34; mysqli_connect&#34;功能。 试试&#34; mysqli _&#34;功能