โค้ดไฟล์ dbconnect.php
<?php
$mysqli
=
new
mysqli(
"localhost"
,
"root"
,
""
,
"test"
);
/* check connection */
if
(
$mysqli
->connect_errno) {
printf(
"Connect failed: %s\n"
,
$mysqli
->connect_error);
exit
();
}
if
(!
$mysqli
->set_charset(
"utf8"
)) {
printf(
"Error loading character set utf8: %s\n"
,
$mysqli
->error);
exit
();
}
Gen Json.php
<?php
header(
"Content-type:application/json; charset=UTF-8"
);
header(
"Cache-Control: no-store, no-cache, must-revalidate"
);
header(
"Cache-Control: post-check=0, pre-check=0"
, false);
require_once
(
"inc/dbconnect.php"
);
$sql
= "
SELECT * FROM province_th WHERE 1 ORDER BY province_id
";
$result
=
$mysqli
->query(
$sql
);
if
(
$result
&&
$result
->num_rows > 0){
while
(
$row
=
$result
->fetch_assoc()){
$json_data
[] =
array
(
"province_id"
=>
$row
[
'province_id'
],
"province_name"
=>
$row
[
'province_name'
]
);
}
}
// แปลง array เป็นรูปแบบ json string
if
(isset(
$json_data
)){
$json
= json_encode(
$json_data
);
if
(isset(
$_GET
[
'callback'
]) &&
$_GET
[
'callback'
]!=
""
){
echo
$_GET
[
'callback'
].
"("
.
$json
.
");"
;
}
else
{
echo
$json
;
}
}
?>
ไม่มีความคิดเห็น:
แสดงความคิดเห็น