JSON 编码正在删除所有其他值

发布于 2024-10-28 00:21:21 字数 1523 浏览 0 评论 0原文

我构建了一个 php 脚本来响应 ajax 数据调用,该数据是来自 mySQL 数据库的两列,格式为“日期”、“值”,日期按每周间隔排列。

最终,我想通过设置会话变量将系列数据保存在 json 编码数组中,将结果返回到调用页面。我不确定这是否是最合适的方法,因为我正在遵循一些在线示例和教程的混杂,所以我只是希望我走在正确的轨道上。

到目前为止,我可以将结果渲染到调用页面上的 html 表中,如下所示,这给出了我期望的结果:

echo "<table border='1'>
<tr>
<th>Date</th>
<th>Value</th>
</tr>";

while($row=mysql_fetch_array($res_Data)) { 
    echo "<tr>";
   echo "<td>".$row['Date']."</td>";
   echo "<td>".$row['Value']."</td>";
   echo "<tr>";  


} 
echo "</table>";

这给出了下表(复制到这里,没有边框):

Date       Value 
2009-07-12 47.09 

2009-07-19 45.48 

2009-07-26 87.03 

2009-08-02 59.96 

2009-08-09 52.82 

2009-08-16 29.20 

但是,当我尝试对数据进行编码时到一个 JSON 数组中,以便稍后可以由调用页面对其进行操作,该系列中的每个其他值都会被删除。 作为创建最终数组对象的中间步骤,

我使用下面的代码,它简单地编码并回显 msql 查询的每一行,首先重置指针。

mysql_data_seek ($res_Data, 0);

while ($row=mysql_fetch_array($res_Data)) { 
      echo json_encode(mysql_fetch_array($res_Data,MYSQL_NUM))."<br/>";
}

这会产生以下输出,其中每隔一周就会被丢弃:

["2009-07-19","45.48"]
["2009-08-02","59.96"]
["2009-08-16","29.20"]
["2009-08-30","99.35"]
["2009-09-13","99.35"]
["2009-09-27","17.17"]
["2009-10-11","276.64"]
["2009-10-25","336.03"]
["2009-11-08","439.28"]
["2009-11-22","383.82"]
["2009-12-06","512.04"]
["2009-12-20","796.64"]
["2010-01-03","1056.55"]

有人知道为什么会发生这种情况吗?

PS我是个菜鸟,所以这可能是显而易见的。

谢谢

I have a php script constructed to respond to an ajax call for data, which is two columns from a mySQL database of the form "Date","Value", with dates following a weekly interval.

Ultimately I want to return the result to the calling page by setting a session variable to hold the series data in a json encoded array. I'm not sure if this is the most appropriate method as I'm following a bit of mish-mash of online example and tutorials, so I'm just hopinig I'm on the right tracks.

So far I can render the results into a html table on the calling page as follows which gives me the results that I would expect:

echo "<table border='1'>
<tr>
<th>Date</th>
<th>Value</th>
</tr>";

while($row=mysql_fetch_array($res_Data)) { 
    echo "<tr>";
   echo "<td>".$row['Date']."</td>";
   echo "<td>".$row['Value']."</td>";
   echo "<tr>";  


} 
echo "</table>";

This gives me the following table (copied here without the border):

Date       Value 
2009-07-12 47.09 

2009-07-19 45.48 

2009-07-26 87.03 

2009-08-02 59.96 

2009-08-09 52.82 

2009-08-16 29.20 

However, when I try to encode the data into a JSON array so that it can be later manipulated by the calling page every other value in the series is dropped. T

As an intermediate step towards creating the final array object I am using the code below which simply encodes and echoes each row of the msql query, having first reset the pointer.

mysql_data_seek ($res_Data, 0);

while ($row=mysql_fetch_array($res_Data)) { 
      echo json_encode(mysql_fetch_array($res_Data,MYSQL_NUM))."<br/>";
}

This results in the following output where every other week is dropped:

["2009-07-19","45.48"]
["2009-08-02","59.96"]
["2009-08-16","29.20"]
["2009-08-30","99.35"]
["2009-09-13","99.35"]
["2009-09-27","17.17"]
["2009-10-11","276.64"]
["2009-10-25","336.03"]
["2009-11-08","439.28"]
["2009-11-22","383.82"]
["2009-12-06","512.04"]
["2009-12-20","796.64"]
["2010-01-03","1056.55"]

Does anyone have any ideas why this might be happening?

Ps I am something of a noobie so it's probably glaringly obvious.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯角 2024-11-04 00:21:21

问题是您的 while 循环正在获取一行,然后在您的 json_encode 调用中,代码正在获取新行。

您应该使用在 json_encode 调用中获取的 $row

while ($row=mysql_fetch_array($res_Data,MYSQL_NUM)) {
    echo json_encode($row)." ";
}

Problem is that your while loop is fetching a row and then in your json_encode call, the code is fetching a new row.

You should use the $row that you fetched in the json_encode call.

while ($row=mysql_fetch_array($res_Data,MYSQL_NUM)) {
    echo json_encode($row)." ";
}
掩耳倾听 2024-11-04 00:21:21

正确的代码是:

while ($row=mysql_fetch_array($res_Data,MYSQL_NUM)) { 
    echo json_encode($row))."
"; }

每次调用 mysql_fetch_array 时都会获取行。

The right code is:

while ($row=mysql_fetch_array($res_Data,MYSQL_NUM)) { 
    echo json_encode($row))."
"; }

Every time you call the mysql_fetch_array the row is fetched.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文