它返回的内容如下:
[
{
"0": "1",
"1": "Udon House",
"2": "food",
"3": "31.5076701",
"4": "74.3544522",
"5": "a trip to the landa.",
"6": "images/udonHouse.jpg",
"place_id": "1",
"place_name": "Udon House",
"category": "food",
"lat": "31.5076701",
"lon": "74.3544522",
"description": "a trip to the landa.",
"db_image": "images/udonHouse.jpg"
},
{
"0": "5",
"1": "Lahore Meuseum",
"2": "attractions",
"3": "31.5681556",
"4": "74.3061492",
"5": "want to discover histroy?",
"6": "images/badshahi.jpg",
"place_id": "5",
"place_name": "Lahore Meuseum",
"category": "attractions",
"lat": "31.5681556",
"lon": "74.3061492",
"description": "want to discover histroy?",
"db_image": "images/badshahi.jpg"
}
]
这是我的PHP代码:
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="tourist"; // Database name
$con = mysqli_connect("$host", "$username", "$password","$db_name")or die("cannot connect");
if($con){
mysqli_set_charset($con ,'utf8');
$user_id = $_POST['user_id'];
$qry =
("SELECT p.*
FROM
places p INNER JOIN
favorites f ON p.place_id = f.place_id INNER JOIN
user u ON u.id = f.user_id
WHERE
u.id = $user_id");
$query=mysqli_query($con ,$qry);
if (!$query) {
$message = 'Invalid query: ' . mysqli_error() . "\n";
$message .= 'Whole query: ' . $qry;
die($message);
}
$return_arr = array();
$num_rows = mysqli_num_rows($query);
if ($num_rows > 0) {
while ($r = mysqli_fetch_array($query)) {
header('Content-Type: application/json');
array_push($return_arr,$r);
}
echo json_encode($return_arr);
}
}
?>
为什么它在一个对象中返回两次行?我只想要名字不带编号的数据我在这里做错了什么? 有任何建议或替代解决方案吗?
答案 0 :(得分:0)
默认,mysqli_fetch_array
函数使用MYSQLI_BOTH
作为“结果类型”。
您需要将第二个参数设置为MYSQLI_ASSOC
,以使其仅返回键名。
while ($r = mysqli_fetch_array($query, MYSQLI_ASSOC)){
array_push($return_arr,$r);
}
P.S。 header('Content-Type: application/json');
只需要被称为一次,因此请将其从while
循环中取出。