php+sqlite+ajax获取id=1的数据


demo-get-detail-for-ajax.php文件:
从数据库中读取数据,并返回一个json(测试用数据库文件test.db从这里下载
<?    
$conn = new SQLiteDatabase('test.db');    
$id= (int) isset($_GET['id']) ? $_GET['id'] : 1;

if($id<1 || $id>29){
  $id=1;
}

$sql = "select id,title,content from www_1500km_com where id=$id ";    
$x = $conn->arrayQuery($sql);  
$d = $x[0];
$a=array('id'=>$d['id'],'title'=>$d['title'],'content'=>$d['content']);
echo json_encode($a);

?>

demo-ajax-detail.html文件:
用jquery解析demo-get-detail-for-ajax.php返回的json:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="http://blog.1500km.com/sites/ui-blog/other/favicon.ico" rel="shortcut icon" />
<link href="http://blog.1500km.com/sites/ui-blog/other/favicon.ico" rel=bookmark />

<style>  
hr{ border-bottom:dashed 1px #ccc; border-top:solid 0px #ccc;}  
.btn{ background-color:#339933; color:#FFFFFF; padding:3px;}  
body{ font-size:12px; color:#666;}
</style>  

<script type="text/javascript" src="http://blog.1500km.com/sites/ui-blog/other/jquery.js"></script>
<script type="text/javascript">
function get_ajax_detail(id){
  var url='/sites/ui-blog/other/demo-get-detail-for-ajax.php?id='+id;
  $.getJSON(url,function(data){
    var id=data.id;
    var title=data.title;
    var content=data.content;
    $("#id").html('<hr>id='+id);
    $("#title").html('<hr>'+title);
    $("#content").html('<hr>'+content+'<hr>');
  });
}

$(document).ajaxStart(function(){ 
  $("#loading").show(); 
}).ajaxStop(function(){ 
  $("#loading").hide(); 
});
</script>

<img src="http://blog.1500km.com/sites/download/images/1500km.png" style="vertical-align:middle;" />php+sqlite+ajax获取某一id值的数据<hr>

<input type="button" class="btn" value="获取id=1的数据" onClick="get_ajax_detail(1)">
<input type="button" class="btn" value="获取id=2的数据" onClick="get_ajax_detail(2)">
<input type="button" class="btn" value="获取id=3的数据" onClick="get_ajax_detail(3)">


<div id="id"></div>
<div id="title"></div>
<div id="content"></div>

<div id="loading" style="display:none">
<img src="http://blog.1500km.com/sites/ui-blog/other/loading.gif">
</div> 

<br><br>
<a target="_blank" href="http://www.1500km.com">返回1500km</a>

程序运行结果如下图所示,点击这里亲自运行看看