php - 使用 PHP 进行 MySQL 查询的 JSONArray 不适用于复杂的查询

标签 php mysql arrays json

我有一个 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 获取结果时,该脚本可以完美运行。这是示例:

enter image description here

我不知道出了什么问题:/你们能帮助我吗?谢谢。

最佳答案

当查询有效但您的脚本无效时,问题出在 @anton86993 提到的 JSON 编码中。

检查结果中是否有特殊字符。这些可能包括元音变音、法国口音等。

要确认这一点,请将结果的内容替换为简单的字符串并运行您的代码。

关于php - 使用 PHP 进行 MySQL 查询的 JSONArray 不适用于复杂的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33857264/

相关文章:

python - 将行从一个数据库复制到另一个数据库并更改一些值

mysql - 显示具有最大行数的不同列

python - 优化双for循环以查找二维数组上特定值的计数

javascript - javascript 中过滤器和映射的奇怪行为

php - 使用 php 搜索。将 keyward 与同一表中的多个列进行比较

PHP 将 null 写入 JSON 文件

PHP、curl 和原始 header

python - MAC | Python 脚本运行良好,在终端插入 Mysql 表但无法作为 Cron 作业运行 |苹果

c++ - C++中数组的指针

php - 如何在 MySQL 中包含一个 PHP 数组变量