我正在努力让搜索工作,我已经回应并使用了var dump,我在浏览器中得到的是:
0没有搜索结果
object(mysqli_result)#2 (5) { ["current_field"]=> int(0)
["field_count"]=> int(4)
["lengths"]=> NULL
["num_rows"]=> int(0)
["type"]=> int(0)
}
但是我输入的是在我的数据库中的巴黎,所以它不应该回应没有搜索。
以下是我的代码。不知道我哪里出错了,任何帮助都会很有意义。
require_once('config1.php');
error_reporting(E_ALL);
$output = '';
if(isset($_POST['search'])){
$searchq = $_POST['search'];
$sql = "SELECT a.attraction_name, a.lat, a.long, a.cost FROM zz_attractions a
INNER JOIN zz_city c ON a.city_id = c.city_id WHERE c.city_name LIKE
'%searchq%'";
$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);
if ($count == 0) {
$output = 'there was no search results';
echo $count;
echo $output;
var_dump($result);
} else {
while ($row = mysql_fetch_array($sql)) {
$attraction_name = $row['attraction_name'];
$lat = $row['lat'];
$long = $row['long'];
$cost = $row['cost'];
$output .= '<div>'.$attraction_name.' '.$lat.' '.$long.' '.$cost.'</div>';
}
}
}
city_id attraction_id attraction_name lat long cost
1 1 Eiffel Tower 49 2 25
答案 0 :(得分:2)
您错过了传递给SQL语句的变量的$ off。尝试
$sql = "SELECT a.attraction_name, a.lat, a.long, a.cost FROM zz_attractions a
INNER JOIN zz_city c ON a.city_id = c.city_id WHERE c.city_name LIKE
'%$searchq%'";
另外,看一下sql语句的绑定参数,而不是直接传递它们,因为你当前的代码让你很容易注入 https://www.w3schools.com/sql/sql_injection.asp | http://php.net/manual/en/mysqli-stmt.bind-param.php