我有一个执行查询的函数,然后创建一个JSON。它工作得很好
function getStmtJson($conn){
$sth = mysqli_query($conn, "SELECT firstname, lastname FROM users");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
}
这是返回值:
[{"firstname":"Lena-Jill","lastname":"Grottburg"},{"firstname":"Rafael"...]}
但是,当我将SQL输入更改为:
时SELECT name, latitude, longitude FROM analysis GROUP BY name, latitude, longitude
我没有得到任何回报。 该查询在phpmyadmin中工作正常。
在表格中,name的类型为varchar,纬度和经度为十进制。
如果从查询中删除了名称,那么它就能正常工作。
编辑:
完整的PHP代码:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "robin";
$input = "get_spots";
$case_one = "get_spots";
$case_two = "input_analysis";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(strcmp($input, $case_one) == 0){
getStmtJson($conn);
echo "<br> <br>";
getSpotList($conn);
}
elseif(strcmp($input, $case_two) == 0){
echo "case_two";
}
else{
echo "else";
}
$conn->close();
function getStmtJson($conn){
$sth = mysqli_query($conn, "SELECT firstname, lastname FROM users");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
}
function getSpotList($conn){
$sth = mysqli_query($conn, "SELECT name, latitude, longitude FROM analysis GROUP BY name, latitude, longitude");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
}