我有一个 PHP 脚本,它从 MySQL 查询返回 JSONAray。它适用于大多数查询,但我需要从特定的长查询中获取结果。
这是 PHP 函数脚本:
public function getTopRecipes() {
$myArray = array();
if ($result = $this->conn->query("SELECT recipe.`unique_id`, recipe.`title`, recipe.`img_tumbnail_link`, recipe.`add_date`, recipe.`kitchen_type`, recipe.`meal_type`, user.`name`, user.`surname`, COUNT(`like`.`recipe_unique_id_fk`) AS like_count
FROM `recipe`
JOIN `like` ON (recipe.`unique_id` = `like`.`recipe_unique_id_fk`)
JOIN `user` ON (user.`unique_id` = recipe.`user_unique_id_fk`)
WHERE recipe.`acceptance` = '1'
GROUP BY `like`.`recipe_unique_id_fk`
ORDER BY like_count DESC")) {
while ($row = $result->fetch_array(MYSQL_ASSOC)) {
$myArray[] = $row;
}
echo json_encode($myArray);
}
$result->close();
}
此脚本返回空白页面,没有任何 JSON 数据。然而,当从 phpMyAdmin 获取结果时,该脚本可以完美运行。这是示例:
我不知道出了什么问题:/你们能帮助我吗?谢谢。
最佳答案
当查询有效但您的脚本无效时,问题出在 @anton86993 提到的 JSON 编码中。
检查结果中是否有特殊字符。这些可能包括元音变音、法国口音等。
要确认这一点,请将结果的内容替换为简单的字符串并运行您的代码。
关于php - 使用 PHP 进行 MySQL 查询的 JSONArray 不适用于复杂的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33857264/