php呼叫資料庫反饋json資料

2021-07-25 09:00:50 字數 1153 閱讀 9765

只是一篇比較簡單的php呼叫資料庫反饋josn資料。

<?php

define("sql_url","localhost");  //我這裡安裝的是apache

define("sql_user","資料庫使用者"); 

define("sql_pass","資料庫密碼");

define("sql_db","查詢資料所在庫");

$id=$_get['id'];

$conn=mysql_connect(sql_url,sql_user,sql_pass) or die('link---sibai'.mysql_error);

mysql_select_db(sql_db,$conn)or die('huo qu shu ju si bai!'.mysql_error);

$result=mysql_query("select * from 查詢表名") or die('chu cuo le!'.mysql_error);

while($row=mysql_fetch_assoc($result))

$json=json_encode($results);

echo "";

mysql_free_result($result);

mysql_close($conn);

?>

這樣就能調出mysql裡的資料並反饋回json資料了。這裡如果資料庫裡存的是中文,它會轉義成unicode編碼!

在php檔案中加上這個方法強制不轉義它就會正常輸出中文了

function json_encode_ex($value)

)#i",

function($matchs)

,$str

);return $str;

}else

}

將這個方法放在剛剛的哪個php檔案的開頭或者結尾呼叫將$json=json_encode($results);換成$json=json_encode_ex($results);

就ok了

PHP 從資料庫中取出資料並存為Json資料

include conn.php sql select from user sql result mysql query sql 執行sql json data array 定義好乙個陣列.php中array相當於乙個資料字典.定義乙個類,用到存放從資料庫中取出的資料.class user whil...

PHP資料庫呼叫類呼叫例項

require once mssql.class.php 1.建立類,並連線資料庫 db new mssql dns aaa uid sa pwd sa dbname test 2.連線資料庫 conn db config dns aaa uid sa pwd sa dbname test 3.選擇...

php實現查詢資料庫返回json資料

示例 一 設定返回json格式資料 連線資料庫 link mysql connect localhost root root or die unable to connect to the mysql mysql query set names utf8 mysql select db jilinw...