我的代码中遇到了$ json变量的问题。出于某种原因,我的php查询没有被编码到我的$ json变量中。
$sql = "SELECT `$column`, `Year`, `Month` FROM unemployed WHERE year BETWEEN ? AND ? and month= ?";
$stmt = $conn->stmt_init();
$stmt->prepare($sql);
$stmt->bind_param('sss', $gYear, $gYear2, $gMonth);
$stmt->bind_result($Column, $year, $month);
$stmt->execute();
$stmt->store_result();
$numRows = $stmt->num_rows;
$json = array();
if ($numRows) { ?>
<table>
<tr>
<th scope="col"> Hi </th>
<th scope="col"> Year </th>
<th scope="col"> Month </th>
</tr>
<?php while ($row = $stmt->fetch()) { ?>
<tr>
<td> <?php echo $Column; ?> </td>
<td> <?php echo $year; ?> </td>
<td> <?php echo $month; ?> </td>
</tr>
$json['unemployed'][]= $row;
<?php } ?>
</table>
<?php
echo json_encode($json);
var_dump($json);
?>
运行此脚本后,我的$ json变量中仍然没有任何内容。有谁知道那是为什么?现在变量不应该是一个带有查询值的数组(每行现在都是数组的一部分)?
答案 0 :(得分:1)
它甚至看起来不像
$json['unemployed'][]= $row;
在php标签内。
此外,看起来fetch()返回boolean。当你调用fetch()时,看起来它用行中的值填充变量$ Column,$ year和$ month。而不是
$json['unemployed'][]= $row;
这样做:
$json['unemployed'][]= array($Column, $year, $month);
答案 1 :(得分:0)
问题在于,在尝试解析PHP代码时,您不在PHP解释器的范围内。
</tr>
$json['unemployed'][]= $row;
到
</tr>
<?php $json['unemployed'][]= $row; ?>