js mysql json字符串转数组中_php数据库数据转换为js中的json对象
/
1.在company數據user表中取出10條數據,保存為數組
2.在將數組轉化為json格式,傳遞給js
3.用json解析器將傳遞過來的json字符串轉化為json對象,
4.用document.write輸出語句打印在頁面上
*/
$conn = mysql_connect(“localhost”,”root”,”root”);???//連接數據庫
mysql_query(“set names utf8″);?? ??? ??? ??? ??? ????//設置編碼
mysql_select_db(“company”,$conn);?? ??? ??? ??? ??? ?//選擇庫
$sql = “select * from user limit 10″;?? ??? ??? ??? ?//在user表中查詢10條數據
$res = mysql_query($sql);?? ??? ??? ??? ??? ??? ??? ?//執行sql語句
$arr = array();
while($row = mysql_fetch_array($res,MYSQL_ASSOC)){???//查詢出來sql
$arr[] = $row;?? ??? ??? ??? ??? ??? ??? ??? ????//將查詢出來的結果賦給數組$arr
}
$str = json_encode($arr);?? ??? ??? ??? ??? ??? ????//將數組轉化為json格式的字符串
?>
var jsonString = ‘<?php echo $str;?>’;?? ??? ??//傳遞php中的數據給js
//document.write(jsonString);
var jsonObject = json_parse(jsonString);?? ??? ??? ?//將json字符串轉化為js中的json對象
for(var i = 0; i < jsonObject.length; i++){?? ??? ??? ?//for循環打印
document.write(“
”);document.write(“
”,jsonObject[i].uid,””);document.write(“
”,jsonObject[i].username,””);document.write(“
”,jsonObject[i].sex,””);document.write(“
”,jsonObject[i].email,””);document.write(“
”);}
總結
以上是生活随笔為你收集整理的js mysql json字符串转数组中_php数据库数据转换为js中的json对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 输出数组函数调用函数,php
- 下一篇: qt mysql乱码_Qt连接mysql