MYSQL数据-> PHP 数组 -> Javascript(或 Jquery)如何使用 JSON 传递数据?

发布于 2024-08-30 23:33:08 字数 1772 浏览 6 评论 0原文

我正在开始一个新网站(这是我的第一个网站),但我遇到了大麻烦! 我编写了这段代码

<?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 技术交流群。

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

发布评论

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

评论(4

花开柳相依 2024-09-06 23:33:08

这不是正确的 JSON。正确的做法是,如果元素用方括号括起来(表示数组),如下所示:

[{"country":"liechtenstein"},
 {"country":"romania"},
 {"country":"jugoslavia"},
 {"country":"polonia"}]

您可以首先从数组中的数据库中获取所有元素,然后对该数组进行编码:

$elements = array()

for ($i=0;$i<$numberOfRows;$i++){
        $elements[]=mysqli_fetch_assoc($result);
}

echo json_encode($elements);

这应该可行(使用 $.getJSON() 在 jQuery 中)。

更新: .getJSON() 示例:

$.getJSON('/path/to/php_file', function(data) {
    // something with data which is of form
    // data = [{'country': '...'}, {...}, ...]
    //e.g.
    alert(data[0].country);
});

It is not correct JSON. Correct would be, if the elements were enclosed in square brackets (indicating an array) like so:

[{"country":"liechtenstein"},
 {"country":"romania"},
 {"country":"jugoslavia"},
 {"country":"polonia"}]

You can first fetch all elements from the DB in an array and then encode this array:

$elements = array()

for ($i=0;$i<$numberOfRows;$i++){
        $elements[]=mysqli_fetch_assoc($result);
}

echo json_encode($elements);

This should work (using $.getJSON() in jQuery).

Update: A .getJSON() example:

$.getJSON('/path/to/php_file', function(data) {
    // something with data which is of form
    // data = [{'country': '...'}, {...}, ...]
    //e.g.
    alert(data[0].country);
});
舂唻埖巳落 2024-09-06 23:33:08

请注意,该 JSON 字符串不是有效的 JSON 字符串!
我建议您在生成输出之前使用 json_encode 一次。
你可能会这样做:

$countries = array();
for ($i=0;$i<$numberOfRows;$i++){
    $row=mysqli_fetch_assoc($result);

    //Not needed, I guess
    //extract($row); 

    $countries[] = $row;

    //More probably, you want to get only the country name
    //$countries[] = $row['country'];
}

$result = json_encode( $countries );
echo $result;

希望它是正确的,我还没有测试过:)

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:

$countries = array();
for ($i=0;$i<$numberOfRows;$i++){
    $row=mysqli_fetch_assoc($result);

    //Not needed, I guess
    //extract($row); 

    $countries[] = $row;

    //More probably, you want to get only the country name
    //$countries[] = $row['country'];
}

$result = json_encode( $countries );
echo $result;

Hope it's correct, I haven't tested it :)

凉薄对峙 2024-09-06 23:33:08

我想你想在循环之后回显一次。另外,如果您想将其作为数组传递,请用方括号将其括起来。像这样的事情:

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"},]

至于 $.getJSON,您如何应用它? getJSON 的语法是:

$.getJSON( url, [ data ], [ callback(data, textStatus) ] )

您需要一个回调函数来使用“数据”

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:

for ($i=0;$i<$numberOfRows;$i++){
        $row=mysqli_fetch_assoc($result);
        extract($row);
        $a=json_encode($row);
        $a=$a.",";
    }
echo '['.$a.']';

The result you would be sending would be:

[{"country":"liechtenstein"},{"country":"romania"},{"country":"jugoslavia"},{"country":"polonia"},]

As to $.getJSON, how are you applying this? The syntax for getJSON is:

$.getJSON( url, [ data ], [ callback(data, textStatus) ] )

You need a callback function to make use of 'data'

古镇旧梦 2024-09-06 23:33:08

你可以尝试这种方式:

PHP/HTML:

<div id="iddiv">
<?php
    echo "{data: [";
    for ($i=0; $i<$numberOfRows; $i++){
        $row=mysqli_fetch_assoc($result);
        extract($row);
        $a = (($i!=0)?",":"") . json_encode($row);
        echo $a;
    }
    echo "]}";
?>
</div>

Javascript:

$(function(){    
    var p = $.getJSON($("#iddiv").text());
    alert(p[0].country);
});

you can try this way:

PHP/HTML:

<div id="iddiv">
<?php
    echo "{data: [";
    for ($i=0; $i<$numberOfRows; $i++){
        $row=mysqli_fetch_assoc($result);
        extract($row);
        $a = (($i!=0)?",":"") . json_encode($row);
        echo $a;
    }
    echo "]}";
?>
</div>

Javascript:

$(function(){    
    var p = $.getJSON($("#iddiv").text());
    alert(p[0].country);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文