PHP& MySQL - 如果value不为null,则循环查询

时间:2016-08-04 20:38:02

标签: php mysql database wordpress

我目前有一个像这样的数据库表设置:

| id | thing_id | value

“thing_id”与同一个表中的唯一ID有关。

我正在尝试创建一个函数(在PHP中),它将对MySQL数据库运行查询,将该信息转储到数组中,如果“thing_id”为NOT NULL,它将循环返回并再次运行查询,并将信息添加到现有阵列。这将持续到“thing_id”为NULL。

我该怎么做呢?顺便说一句,我是在WordPress上设计的。

以下内容有效,但我觉得有更好的方法可以做到这一点。我怎样才能简化这一点并使其不占用资源?这就是我所拥有的:

        $related_thingsSql = "
                            SELECT $thingsDB.value, $thingsDB.thing_id, $thingsDB.id
                            FROM $thingsDB
                            WHERE $thingsDB.id = '$related_thing_id'
                            ";              

        $related_things = $wpdb->get_results( "$related_thingsSql" );

        foreach ($related_things as $related_thing) {
            $related_thing_name[] = $related_thing->value;  
            $related_thing_id2[] = $related_thing->id;
            $related_thing_id = $related_thing->thing_id;                   
        }
        while ($related_thing_id != NULL) {
            $related_thingsSql2 = "
                                    SELECT $thingsDB.value, $thingsDB.thing_id, $thingsDB.id
                                    FROM $thingsDB
                                    WHERE $thingsDB.id = '$related_thing_id'
                                ";                                              
            $related_things = $wpdb->get_results( "$related_thingsSql2" );
            foreach ($related_things as $related_thing) {
                array_unshift($related_thing_name, $related_thing->value);
                array_unshift($related_thing_id2, $related_thing->id);
                $related_thing_id = $related_thing->thing_id;
                if (empty($related_thing->thing_id)) {
                    $related_thing_id = NULL;   
                }
            }
        }
        $related_things_length = count($related_thing_name);

        for ($x = 0; $x < $related_things_length; $x++) {
            echo ' > <a href="' . site_url() . '?id=' . $related_thing_id2[$x] . '">' . $related_thing_name[$x] . '</a>';
        }

1 个答案:

答案 0 :(得分:0)

我想出了一种有效的方法。我觉得这种方式真的很草率,但它确实有效。如果有人对如何改进这一点有任何建议,请随意。

            $related_thingsSql = "
                                SELECT $thingsDB.value, $thingsDB.thing_id, $thingsDB.id
                                FROM $thingsDB
                                WHERE $thingsDB.id = '$related_thing_id'
                                ";              

            $related_things = $wpdb->get_results( "$related_thingsSql" );

            foreach ($related_things as $related_thing) {
                $related_thing_name[] = $related_thing->value;  
                $related_thing_id2[] = $related_thing->id;
                $related_thing_id = $related_thing->thing_id;                   
            }
            while ($related_thing_id != NULL) {
                $related_thingsSql2 = "
                                        SELECT $thingsDB.value, $thingsDB.thing_id, $thingsDB.id
                                        FROM $thingsDB
                                        WHERE $thingsDB.id = '$related_thing_id'
                                    ";                                              
                $related_things = $wpdb->get_results( "$related_thingsSql2" );
                foreach ($related_things as $related_thing) {
                    array_unshift($related_thing_name, $related_thing->value);
                    array_unshift($related_thing_id2, $related_thing->id);
                    $related_thing_id = $related_thing->thing_id;
                    if (empty($related_thing->thing_id)) {
                        $related_thing_id = NULL;   
                    }
                }
            }
            $related_things_length = count($related_thing_name);

            for ($x = 0; $x < $related_things_length; $x++) {
                echo ' > <a href="' . site_url() . '?id=' . $related_thing_id2[$x] . '">' . $related_thing_name[$x] . '</a>';
            }