$con = mysql_connect("localhost", "db163810_f", "1e6969e2");
if (!$con)
{
die('不能建立連接: ' . mysql_error());
}
$db_selected = mysql_select_db("db163810",$con);
mysql_query("SET NAMES 'utf8'");
if (!$db_selected)
{
die ("這個數據庫不能被選: " . mysql_error());
}
$sql = "SELECT `ID` , `post_date` , `post_title` , `post_content` FROM `wp_posts` order by `post_date` desc LIMIT 0, 3 ";
$result = mysql_query($sql,$con);
//print_r($result); //Resource id #2
//print($result); //Resource id #2
//echo $result; //Resource id #2
//echo "===================result==============================
";
while($row = mysql_fetch_assoc($result))
{
//print_r(json_encode($row));
//print(json_encode($row));
echo json_encode($row);
echo "
=====================row================================
";
}
mysql_close($con);
?>
mysql數據庫權限,只允許localhost連接,不能通過Internet連接
字符集轉換
查詢結果集不能直接輸出,print($result),緊緊輸出資源代碼Resource id #2
輸出了3個JSON字符串,3對大括弧
3,android客戶端如何解碼?
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("htttp:localhost/mysql.php");
httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//params:List php接收
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
return new JSONObject(json);