Php simple implementation query database return json data _ PHP Tutorial

Source: Internet
Author: User
Php simply queries the json data returned by the database. Php simple implementation query database return json data example code 1: 12345678910111213141516171819202122232425262728293031323334353637 setting return php simple implementation query database return json data

Sample Code 1:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

// Set the returned json data

Header ('content-type: application/json; charset = utf8 ');

// Connect to the database

$ Link = mysql_connect ("localhost", "root", "root") or die ("Unable to connect to the MySQL! ");

Mysql_query ("set names 'utf8 '");

Mysql_select_db ("jilinwula", $ link) or die ("Unable to connect to the MySQL! ");

// Obtain paging parameters

$ Page = 0;

$ PageSize = 3;

If (! Is_null ($ _ GET ["page"]) {

$ Page = $ _ GET ["page"];

}

If (! Is_null ($ _ GET ["pageSize"]) {

$ PageSize = $ _ GET ["pageSize"];

}

// Query data to the array

$ Result = mysql_query ("select username, password from userinfo limit". $ page. ",". $ pageSize ."");

$ Results = array ();

While ($ row = mysql_fetch_assoc ($ result )){

$ Results [] = $ row;

}

// Convert the array to json format

Echo json_encode ($ results );

// Close the connection

Mysql_free_result ($ result );

Mysql_close ($ link );

Sample Code 2:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

// SQL statement to be executed

// Single

$ SQL = "select id, name from tbl_user where id = 1 ";

// Multiple data entries

// $ SQL = "select id, name from tbl_user ";

// Call the conn. php file for database operations

Require ('Conn. php ');

// Message indicating successful operation. note: $ result exists in the conn. php file and is called.

If ($ result)

{

// $ Array = mysql_fetch_array ($ result, MYSQL_ASSOC );

/* Dataset

$ Users = array ();

$ I = 0;

While ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC )){

Echo $ row ['id']. '-----------'. $ row ['name'].'
';

$ Users [$ I] = $ row;

$ I ++;

}

Echo json_encode (array ('datalist' => $ users ));

*/

/* Single data entry */

$ Row = mysql_fetch_row ($ result, MYSQL_ASSOC );

Echo json_encode (array ('jsonobj '=> $ row ));

}

Mysql_free_result ($ result );

// Release result

Mysql_close ();

// Close the connection

?>

Example code 1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 28 29 30 31 32 33 34 35 36 37 // Set return...

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.