0

在我的 PHP 文件中,我正在运行以下 SQL 查询:

SELECT *   
FROM national_age_gender_demographics INNER JOIN arizona_age_gender_demographics
WHERE national_age_gender_demographics.age_group = arizona_age_gender_demographics.age_group
ORDER BY national_age_gender_demographics.index_number";

现在,我正在尝试使用以下内容访问行以创建 JSON 数据:

for ($i = 0; $i < $numRows; $i++) {
    if ($i != 0) {
        $tableData .= ",[";
    }
    $tableData .= '"' . $row['national_age_gender_demographics.age_group'] . '",';
    $tableData .= '"' . $row['national_age_gender_demographics.both_pop'] . '",';
    $tableData .= '"' . $row['national_age_gender_demographics.male_pop'] . '",';
    $tableData .= '"' . $row['national_age_gender_demographics.female_pop'] . '",';
    $tableData .= '"' . $row['national_age_gender_demographics.male_percent'] . '",';
    $tableData .= '"' . $row['national_age_gender_demographics.female_percent'] . '",';
    $tableData .= '"' . $row['national_age_gender_demographics.both_percent'] . '",';
    $tableData .= '"' . $row['national_age_gender_demographics.males_per_100_females'] . '",';
    $tableData .= '"' . $row['arizona_age_gender_demographics.both_pop'] . '",';
    $tableData .= '"' . $row['arizona_age_gender_demographics.male_pop'] . '",';
    $tableData .= '"' . $row['arizona_age_gender_demographics.female_pop'] . '",';
    $tableData .= '"' . $row['arizona_age_gender_demographics.male_percent'] . '",';
    $tableData .= '"' . $row['arizona_age_gender_demographics.female_percent'] . '",';
    $tableData .= '"' . $row['arizona_age_gender_demographics.both_percent'] . '",';
    $tableData .= '"' . $row['arizona_age_gender_demographics.males_per_100_females'] . '"]';
    if ($i != $numRows - 1) {
        $row = mysqli_fetch_array($result);
    }
}

在我的浏览器中输入 PHP 文件的 URL 时,我尝试访问连接表的每一行都得到未定义的索引。

我想知道,访问连接表的正确语法是什么?不,不要告诉我使用 json_encode,因为它会在 DataTables 中引发 JavaScript 错误。

4

1 回答 1

0

你不能有.列名,所以我假设第一部分实际上是表的名称。在这种情况下,您的数组键是句点之后的部分:

$row['males_per_100_females']

如果你要输出 JSON,你应该直接使用json_encode而不是手动编码。

$data = array(
   'some_key' => 'some data',
   'table_data' => array()
);

while(false !== ($row = $result->fetch_assoc())) {
   $data['table_data'][] = $row;
}

echo json_encode($data);
于 2013-04-29T20:33:28.063 回答