PHP array and JS interaction problems

Source: Internet
Author: User
PHP array and JS interaction problem younger brother wants to use AJAX to fill data
$ SQL = "Select * from nation _fance where ". $ condition; $ rs = mysql_query ($ SQL, $ conn); $ index = 0; while ($ row = mysql_fetch_array ($ rs ))! = False) {$ result [$ index] = array ("PK" => $ row ["PK"], "NAME" => $ row ['name'], "SHORT_NAME" => $ row ['short _ name'], "NODE" => $ row ['node'], "PARENT_NODE" => $ row ['parent _ node']); $ index ++; // The following method cannot be used. is mysql_fetch_array an array returned? // $ Result [$ index] = $ row ;}


Please kindly advise, PHP + JQUERY, how to transmit data, how is it most convenient, and how to obtain it on the JS end. thank you...


Reply to discussion (solution)

$ Result [$ index] = $ row;
Why not? Only doubled the data. Does not affect usage

Finally, echo json_encode ($ result);. js can be interpreted.

Upstairs boss, I echo json_encode ($ result); this line of code is there.

How does JS read data? can you give an example?

It's not how JavaScript reads data, but what kind of data is required by js.
Difficulties cannot be left to others

Finally, echo json_encode ($ result);. js can be interpreted.


I am also a new student. Should you generate json?


Finally, echo json_encode ($ result);. js can be interpreted.


I am also a new student. Should you generate json?


Yes

$ Result [$ index] = $ row; this sentence is also acceptable. you can see it in print_r ($ result. It only contains the associated and index arrays at the same time.

Php and JS are of course the most convenient to transmit data in JSON. You can write as follows:

While ($ row = mysql_fetch_assoc ($ rs ))! = False ){
$ Result [] = $ row;
}
Echo json_encode ($ result );

Then JSON can be processed in JS.

The problem is not my code problem, it is a Chinese problem, it needs to be converted... everything is OK
$ Result = array ();
While ($ row = mysql_fetch_assoc ($ rs ))! = False ){
$ Result [] = array ("PK" => $ row ["PK"],
"NODE" => $ row ["NODE"],
"NAME" => iconv ("gb2312", "UTF-8", $ row ["NAME"])
);
}

Echo json_encode ($ result );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.