MYSQL数据-> PHP 数组 -> Javascript(或 Jquery)如何使用 JSON 传递数据?
我正在开始一个新网站(这是我的第一个网站),但我遇到了大麻烦! 我编写了这段代码
<?php
include("misc.inc");
$cxn=mysqli_connect($host,$user,$password,$database) or die("couldn't connect to server");
$query="SELECT DISTINCT country FROM stamps";
$result=mysqli_query($cxn,$query) or die ("couldn't execute query");
$numberOfRows=mysqli_num_rows($result);
for ($i=0;$i<$numberOfRows;$i++){
$row=mysqli_fetch_assoc($result);
extract($row);
$a=json_encode($row);
$a=$a.",";
echo $a;
}
?>
,输出如下:
{"country":"liechtenstein"},{"country":"romania"},{"country":"jugoslavia"},{"country":"polonia"},
这应该是正确的 JSON 输出...
我现在如何在 Jquery 中获取它?我尝试过
$.getJSON
,但无法正确融合它。我还不想将数据传递给 DIV 或 HTML 中的类似内容。
作为更新,Andres Descalzo 的代码可以工作了!
<?php
include("misc.inc");
$cxn=mysqli_connect($host,$user,$password,$database) or die("couldn't connect to server");
$query="SELECT DISTINCT country FROM stamps";
$result=mysqli_query($cxn,$query) or die ("couldn't execute query");
$numberOfRows=mysqli_num_rows($result);
echo "{data: [";
for ($i=0; $i<$numberOfRows; $i++){
$row=mysqli_fetch_assoc($result);
extract($row);
$a = (($i!=0)?",":"") . json_encode($row);
echo $a;
}
echo "]}";
?>
输出正确,如下所示:
{data: [{"country":"liechtenstein"},{"country":"romania"},{"country":"jugoslavia"},{"country":"polonia"}]}
如何使用 $getJSON
?
语法
$.getJSON( url, [ data ], [ callback(data, textStatus) ] )
和 url 都是上面提到的 PHP 文件就可以了,但是 [data]
和回调函数呢?
I am starting with a new site (it's my first one) and I am getting big troubles !
I wrote this code
<?php
include("misc.inc");
$cxn=mysqli_connect($host,$user,$password,$database) or die("couldn't connect to server");
$query="SELECT DISTINCT country FROM stamps";
$result=mysqli_query($cxn,$query) or die ("couldn't execute query");
$numberOfRows=mysqli_num_rows($result);
for ($i=0;$i<$numberOfRows;$i++){
$row=mysqli_fetch_assoc($result);
extract($row);
$a=json_encode($row);
$a=$a.",";
echo $a;
}
?>
and the output is as follows :
{"country":"liechtenstein"},{"country":"romania"},{"country":"jugoslavia"},{"country":"polonia"},
which should be a correct JSON outout ...
How can I get it now in Jquery ? I tried with
$.getJSON
but I am not able to fuse it properly. I don't want yet to pass the data to a DIV or something similar in HTML.
As an update, the code of Andres Descalzo works !
<?php
include("misc.inc");
$cxn=mysqli_connect($host,$user,$password,$database) or die("couldn't connect to server");
$query="SELECT DISTINCT country FROM stamps";
$result=mysqli_query($cxn,$query) or die ("couldn't execute query");
$numberOfRows=mysqli_num_rows($result);
echo "{data: [";
for ($i=0; $i<$numberOfRows; $i++){
$row=mysqli_fetch_assoc($result);
extract($row);
$a = (($i!=0)?",":"") . json_encode($row);
echo $a;
}
echo "]}";
?>
The output is correct and is as follows :
{data: [{"country":"liechtenstein"},{"country":"romania"},{"country":"jugoslavia"},{"country":"polonia"}]}
How can I use $getJSON
?
It's ok that the syntax is
$.getJSON( url, [ data ], [ callback(data, textStatus) ] )
and that the url is the above mentioned PHP file but [data]
and callback function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不是正确的 JSON。正确的做法是,如果元素用方括号括起来(表示数组),如下所示:
您可以首先从数组中的数据库中获取所有元素,然后对该数组进行编码:
这应该可行(使用
$.getJSON()
在 jQuery 中)。更新:
.getJSON()
示例:It is not correct JSON. Correct would be, if the elements were enclosed in square brackets (indicating an array) like so:
You can first fetch all elements from the DB in an array and then encode this array:
This should work (using
$.getJSON()
in jQuery).Update: A
.getJSON()
example:请注意,该 JSON 字符串不是有效的 JSON 字符串!
我建议您在生成输出之前使用 json_encode 一次。
你可能会这样做:
希望它是正确的,我还没有测试过:)
Pay attention, the JSON string is not a valid JSON string!
I suggest you to use json_encode once, just before producing the output.
You'd probably do that:
Hope it's correct, I haven't tested it :)
我想你想在循环之后回显一次。另外,如果您想将其作为数组传递,请用方括号将其括起来。像这样的事情:
您要发送的结果将是:
至于 $.getJSON,您如何应用它? getJSON 的语法是:
您需要一个回调函数来使用“数据”
I think you want to echo once - after the loop. Also, if you want to pass it as an array, surround it with brackets. Something like this:
The result you would be sending would be:
As to $.getJSON, how are you applying this? The syntax for getJSON is:
You need a callback function to make use of 'data'
你可以尝试这种方式:
PHP/HTML:
Javascript:
you can try this way:
PHP/HTML:
Javascript: