【问题标题】:Working with arrays in Kohana, Blank page?在 Kohana 中使用数组,空白页?
【发布时间】:2011-03-05 22:53:17
【问题描述】:

tl;dr - 推送一个数组(通过 $array[] 或 $array[$id] 在 Kohana 3 中不起作用,它会给出一个空白页。

我正在使用 Kohana (3),这是我第一次使用 MVC,到目前为止感觉很棒;但是,我正在使用数据库并遇到了一个奇怪的问题,我希望有人能对此有所了解:

我的工作流程是这样的,让您了解我的问题:

$sql = "SELECT table1.row1, max(table2.row1) as `maxAwesome` FROM table1, table2 WHERE table1.id=table2.table1id GROUP BY table1.id";
$table1Results = DB::query(Database::SELECT, $sql)->execute();
$masterArray = array();
foreach ($table1Results as $result1)
{
     $sql = "SELECT * FROM table2 WHERE table2id='"  . $result1['id'] . "' AND column > 21";
     $table2Results = DB::query(Database::SELECT, $sql)->execute();
     $subArray = array();
     foreach ($table2Results as $result2)
     {
         $subArray[$result1['id']] = $result2;
         // Even had just $subArray[] = array("whatever");
     }
     $masterArray[] = array("table1Data" => array(), "table2Data"=> $subArray);
}

我做一个查询,我运行几个最大/最小函数,然后在 foreach 中做一个查询,做另一个选择,以构建一个按我想要的方式格式化的主数据数组,所有 SQL 等都工作得很好,花花公子;但是,当我推送数组时,问题就出现了。

似乎每当我通过 $array[] = array("data"); 推送数组时或通过指定键 $array[$id] = array("data"); Kohana 给了我一个直接的空白页,没有错误,没有输出等。

有时我会收到一个 Kohana 错误,表明密钥不存在(呃,我正在创建它),但大多数情况下输出是纯白色的。

为什么会这样?我是不是搞错了?

提前致谢。


清晰编辑:

我的SQL出错了,问题在于二级数组的构建,例如:

    $queryStores = "SELECT stores.store_id, stores.title, max(product.discount) as `max_discount`, min(product.discount) as `min_discount`
                    FROM stores, products
                    WHERE products.store=stores.store_id
                    GROUP BY products.store";

    $stores = DB::Query(Database::SELECT, $queryStores)->execute();
    $formattedStores = array();
    if (count($stores))
    {
        foreach ($stores as $store)
        {
            $formattedStores[$store['store_id']] = array(
                "title" => $store['title'],
            );
            // Same result if just doing $formattedStores[] = array();
            // Problem goes away should I do:
            // $formattedStores = array("This works");
            //
        }
    }

    echo "<pre>";
    print_r($formattedStores);
    echo "</pre>";

这不打印一个数组,它只是给出一个空白页;但是,如果我将其更改为只是将 $formattedStores 数组重新设置为我得到输出的东西。推送导致问题(可能是 Kohana 错误)的数组是什么?

谢谢

【问题讨论】:

    标签: php mysql arrays kohana kohana-3


    【解决方案1】:

    你的代码应该是这样的:-

    $sql = "SELECT table1.id, table1.row1, max(table2.row1) as `maxAwesome`
            FROM table1, table2
            WHERE table1.id = table2.table1id
            GROUP BY table1.id";
    $table1Results = DB::query(Database::SELECT, $sql)->execute();
    $masterArray = array();
    
    if (count($table1Results))
    {
        foreach ($table1Results as $result1)
        {
            $sqlInner = "SELECT * FROM table2
                         WHERE table2id = '" . $result1['id'] . "'
                           AND column > 21";
            $table2Results = DB::query(Database::SELECT, $sqlInner)->execute();
            $subArray = array();
    
            if (count($table2Results))
            {
                foreach ($table2Results as $result2)
                {
                    $subArray[$result1['id']] = $result2;
                    // Even had just $subArray[] = array("whatever");
                }
            }
    
            $masterArray[] = array("table1Data" => array(), "table2Data"=> $subArray);
        }
    }
    

    一些有价值的编码标准和失误:-

    • 第一个 SQL 中缺少“id”字段(w.r.t.“table1”数据库表)。
    • 第二个 SQL 最好使用另一个变量命名来编写,以便与第一个分开。因此第二个变量被命名为“$sqlInner”。
    • 检查数组变量中是否存在任何数组元素总是更好,所以我使用了使用“if”语句的简单检查。

    希望对你有帮助。

    【讨论】:

    • 我的 SQL 出错了,即使我做了一个简单的单个查询并尝试使用 $array[] = "stuff";我似乎得到一个空白的白色输出并且没有错误。我将在上面编辑一些更清晰的 SQL/Setup 来说明我的问题。谢谢。
    【解决方案2】:

    我确定这与内存有关。

    【讨论】:

    • 如果这是答案,请标记为已选择,否则它将一直弹出为未回答。
    猜你喜欢
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    相关资源
    最近更新 更多