php—如何通过mysql外推数据并放入数组?

6ss1mwsb  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(375)

大家好,我试过很多次了,但我不明白问题出在哪里。。。
我有一个 Ajax Call 传递用于执行查询的参数 MySql . 我不明白为什么不工作并返回空值。

include_once("../config.php"); //file to connect to database
$con= mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_DATA);

//parameters (these are ok)
$ieri = strval($_GET['ieri']);
$oggi = strval($_GET['oggi']);

$magn_min = strval($_GET['magn-min']);
$magn_max = strval($_GET['magn-max']);

$ipo_min = strval($_GET['ipo-min']);
$ipo_max = strval($_GET['ipo-max']);

$lat_sup = strval($_GET['lat-sup']);
$lat_inf = strval($_GET['lat-inf']);

$lng_sin = strval($_GET['lng-sin']);
$lng_des = strval($_GET['lng-des']);

$id_call = $_GET['id-call'];
$offset = ($id_call - 1)*400;

if(isset($_GET['lat-sup'])) {   
 $query = "SELECT * FROM earthquakes WHERE milliseconds BETWEEN " .$ieri." AND " .$oggi." AND magnitude BETWEEN " .$magn_min." AND " .$magn_max." AND ipocentro BETWEEN " .$ipo_min." AND " .$ipo_max." AND latitude BETWEEN " .$lat_inf." AND " .$lat_sup." AND longitude BETWEEN " .$lng_sin." AND " .$lng_des." OFFSET ".$offset." LIMIT 400";
 $n_quakes = "SELECT COUNT(*) FROM earthquakes WHERE milliseconds BETWEEN " .$ieri." AND " .$oggi." AND magnitude BETWEEN " .$magn_min." AND " .$magn_max." AND ipocentro BETWEEN " .$ipo_min." AND " .$ipo_max." AND latitude BETWEEN " .$lat_inf." AND " .$lat_sup." AND longitude BETWEEN " .$lng_sin." AND " .$lng_des;
}
else{
 $query = "SELECT * FROM earthquakes WHERE milliseconds BETWEEN " .$ieri." AND " .$oggi." AND magnitude BETWEEN " .$magn_min." AND " .$magn_max." AND ipocentro BETWEEN " .$ipo_min." AND " .$ipo_max." OFFSET ".$offset." LIMIT 400";
 $n_quakes = "SELECT COUNT(*) FROM earthquakes WHERE milliseconds BETWEEN " .$ieri." AND " .$oggi." AND magnitude BETWEEN " .$magn_min." AND " .$magn_max." AND ipocentro BETWEEN " .$ipo_min." AND " .$ipo_max;
}

$result = mysqli_query($con,$query);
print_r($result); //return null

while ($row = mysqli_fetch_assoc($result)) { 
   $array_quakes[] = $row;
}
mysqli_free_result($array_quakes);

echo json_encode($n_quakes,$array_quakes); 
mysqli_close($con); // close connection with database

为什么? print_r($result); 返回空值?但是我怀疑select count(*)计算返回的行数是不正确的。。。
你能帮助我吗?
非常感谢,对不起我的英语。。。

33qvvth1

33qvvth11#

这可能是缺少括号。把你的衣服包起来 between 括号中的条件

$query = "SELECT * 
FROM earthquakes 
WHERE (milliseconds BETWEEN `$ieri` AND `$oggi`)
    AND (magnitude BETWEEN `$magn_min` AND `$magn_max`)
    AND (ipocentro BETWEEN `$ipo_min` AND `$ipo_max`)
OFFSET $offset LIMIT 400";

$n_quakes = "SELECT COUNT(*)
FROM earthquakes
WHERE (milliseconds BETWEEN `$ieri` AND `$oggi`)
   AND (magnitude BETWEEN `$magn`_min AND `$magn_max`)
   AND (ipocentro BETWEEN `$ipo_min` AND `$ipo_max`)
   AND (latitude BETWEEN `$lat_inf` AND `$lat_sup`)
   AND (longitude BETWEEN `$lng_sin` AND `$lng_des`)";

相关问题