PHP Simple implementation Query database return JSON data _php tutorial

Source: Internet
Author: User

PHP Simple implementation Query database return JSON data


Example code one:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

Sets the return JSON format data

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

Connecting to a 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!");

Get Paging parameters

$page = 0;

$pageSize = 3;

if (!is_null ($_get["page"])) {

$page = $_get["page"];

}

if (!is_null ($_get["PageSize")) {

$pageSize = $_get["PageSize"];

}

Querying the data into the array

$result = mysql_query ("Select Username,password from UserInfo limit". $page. ", ". $pageSize. "");

$results = Array ();

while ($row = Mysql_fetch_assoc ($result)) {

$results [] = $row;

}

Convert an array to JSON format

echo Json_encode ($results);

Close connection

Mysql_free_result ($result);

Mysql_close ($link);

Example code two:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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 statements that need to be executed

Single

$sql = "Select Id,name from Tbl_user where id=1";

More than one piece of data

$sql = "Select Id,name from Tbl_user";

Invoking the conn.php file for database operations

Require (' conn.php ');

Prompt Operation success Information, note: $result exists in the conn.php file, is called out

if ($result)

{

$array =mysql_fetch_array ($result, MYSQL_ASSOC);

/* Data Set

$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 */

$row =mysql_fetch_row ($result, MYSQL_ASSOC);

echo json_encode (Array (' jsonobj ' = $row));

}

Mysql_free_result ($result);

Release results

Mysql_close ();

Close connection

?>

http://www.bkjia.com/PHPjc/985142.html www.bkjia.com true http://www.bkjia.com/PHPjc/985142.html techarticle PHP Simple implementation Query database return JSON Data sample code one: 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 back ...

  • Related Article

    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.