【问题标题】:Turn 4-level adjacency database into array and then into JSON format [duplicate]将4级邻接数据库转换为数组然后转换为JSON格式[重复]
【发布时间】:2020-02-22 16:13:01
【问题描述】:

我正在努力学习,所以在我用尽所有其他途径之前我不会寻求帮助。我会很感激这里的一些帮助/指导,因为solutions I found on Stack Overflow 之一(靠近页面底部)似乎对我不起作用。几个小时后,我准备认输!我发现参考资料告诉我,随着时间的推移,各种扩展已被完全贬值和/或删除,所以我现在想知道这段代码 sn-p 是否更旧并且没有出于不同的原因运行(尽管我知道我也可能遗漏了一些东西)。作为参考,我在共享服务器上,因此无法更改 PHP 版本 (5.6.40) 或 mySQL 版本 (5.6.46)。

我的表被命名(用于测试)test_json。我只有三列:idparentname。邻接数据最大为 4 深。我正在尝试构建一个可以转换的数组(我在想json_encode($arr))。根据我复制/粘贴/修改的代码 sn-p,我认为它会起作用,但即使在使用 print_rvar_dump 之后,屏幕上什么也没有。在 Chrome 开发工具中没有连接错误。

这是我正在使用的代码:

echo '<pre>';

$categories = OrgChart::getTopCategories();
print_r($categories);

echo '</pre>';

class OrgChart
{

    public static function getTopCategories()
    {
        return self::getCategories('parent = 0');
    }

    public static function getCategories($where = '')
    {
        if ($where) $where = " WHERE $where";
        $result = mysql_query("SELECT * FROM test_json $where");

        $categories = array();

        while ($category = mysql_fetch_array($result)){
        $my_id = $category['id'];
        $category['children'] = OrgChart::getCategories("parent = $my_id");
            $categories[] = $category;
        }

        mysql_free_result($result);
        return $categories;
    }
}

经过几个小时探索选项后的更新:

Christos 和 Dharman 是对的,我尝试使用的代码有许多已贬值(已删除?)的扩展。现在我已经修复了这些并且有零错误(使用 error_reporting(E_ALL); ini_set("display_errors", 1); ini_set('display_startup_errors', 1);) 我仍然不明白为什么我只得到这个输出: 大批 ( )

从下面的代码。关于我所缺少的任何其他建议?感谢您提供的任何帮助。我可能正看着它,但我的眼睛开始交叉。

echo '<pre>';

$categories = OrgChart::getTopCategories();
print_r($categories);

echo '</pre>';

class OrgChart
{
    public static function getTopCategories()
    {
        return self::getCategories('parent = 0');
    }

    public static function getCategories($where = '')
    {
        if ($where) $where = " WHERE $where";
//      $result = mysqli_query(&db, "SELECT * FROM test_json $where");
        Database::initialize();
        $result = mysqli_query (Database::$conn, "SELECT * FROM test_json $where");
        $categories = array();

        while ($category = mysqli_fetch_array($result)){
        $my_id = $category['id'];
        $category['children'] = OrgChart::getCategories("parent = $my_id");
            $categories[] = $category;
        }

//  mysql_free_result($result);
    return $categories;

    }
}

【问题讨论】:

  • 如果屏幕上什么也没有,那么可能是因为禁用了错误而导致的 PHP 错误。尝试在脚本开头使用error_reporting(E_ALL); ini_set("display_errors", 1);
  • 你好克里斯托斯。谢谢你。我已将其加载到我的脚本中,它显示错误。我将探索并回顾我的发现。非常感谢!
  • 你好。似乎我在 SELECT 语句中收到错误(未定义变量:mysqli),即使我在连接到 SQL DB 时声明了它($mysqli = new mysqli($hn, $un, $pw, $db);) .查询在 PHP 类语句中是否有所不同?我认为 PHP '$' 使它成为全球性的。 $query = "SELECT * FROM test_json $where"; $result = $mysqli->query($query);
  • 你好。我可能是 Stack Overflow 的新手,这意味着我可能并不完全了解一切是如何运作的,但是 - 我认为 - 这个网站是为具有不同经验水平的人准备的。在挣扎了几个小时并最终屈服并要求SO寻求帮助后获得“反对票”似乎非常苛刻。有人愿意帮助我了解我做了什么/没做什么,有人觉得有义务否决我的问题吗?谢谢。

标签: php mysql json


【解决方案1】:

没有收到任何人的回复,所以我一直在逐行探索。为了解决,这是我发现的:

在我的代码中,返回 self::getCategories('parent = 0');是罪魁祸首。使用“0”或使用“= NULL”没有产生任何结果。我确信这是众所周知的(但不是对我来说,这就是我寻求帮助的原因),但是在为此类查询使用 NULL 值时,您必须使用“IS NULL”或“IS NOT NULL”。我发布此内容以防其他人(例如我)目前不知道此内容。

【讨论】:

    猜你喜欢
    • 2019-03-09
    • 2018-03-11
    • 1970-01-01
    • 2017-07-12
    • 2016-08-05
    • 2014-06-11
    • 2012-02-08
    • 2014-02-18
    • 2011-07-03
    相关资源
    最近更新 更多