<?php
$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==============================<br/>";
while($row = mysql_fetch_assoc($result))
{
//print_r(json_encode($row));
//print(json_encode($row));
echo json_encode($row);
echo "<br/>=====================row================================<br/>";
}
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<NameValuePair> 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);
http://www.cnblogs.com/LIANQQ/archive/2012/11/14/2769911.html