MySQL数据读取,ExtJS和PHP Json


  本文标签:MySQL数据读取

  以下的文章主要讲述的是ExtJS和PHP Json、MySQL数据读取的实际应用,种种包括建立数据库、注册表,建立jsonreader.php和get.php与extjs文件json.js编写,以下就是文章的主要内容描述  。

  ExtJS与PHP Json、MySQL数据读取 1 建立数据库、注册表

  

  1. create database test;  
  2. create table test.login(  
  3. id int primary key,  
  4. name varchar(20) not null,  
  5. password varchar(20) not null  
  6. );  
  7. insert into test.login values  
  8. (1,hong,1234),  
  9. (2,linxiang,1234),  
  10. (3,chen,99a9s),  
  11. (4,luxi,aabe2);  

  

  

  ExtJS与PHP Json、MySQL数据读取 2 建立jsonreader.php和get.php

  jsonreader.php调用json.js

  get.php读取数据库数据

  

  1. jsonreader.php => 
  2. <html> 
  3. <head> 
  4. <title>注册title> 
  5. <link rel="stylesheet" type="text/css" href="../ext/resources/css/ext-all.css" /> 
  6. <script type="text/javascript" src="../ext/adapter/ext/ext-base.js">script> 
  7. <script type="text/javascript" src="../ext/ext-all.js">script> 
  8. <script type="text/javascript" src="json.js">script> 
  9. head> 
  10. <body> 
  11. <div id=grid>div> 
  12. body> 
  13. html> 
  14. get.php=> 
  15. php 
  16. $conn=MySQL_connect("localhost","root","123");  
  17. MySQL_select_db("test");  
  18. $sql="select id,name,password from login";  
  19. $result=MySQL_query($sql,$conn);  
  20. while($row=MySQL_fetch_array($result))  
  21. {  
  22. $arr4[]=$row;  
  23. }  
  24. echo json_encode($arr4);  
  25. ?> 

  

  ExtJS与PHP Json、MySQL数据读取 3 extjs文件json.js编写

  1. json.js=> 
  2. Ext.onReady(function() {  
  3. store=new Ext.data.JsonStore({  
  4. url:get.php,  
  5. data:[],  
  6. fields:[  
  7. {name:id},  
  8. {name:name},  
  9. {name:password}  
  10. ]  
  11. });  
  12. store.load();  
  13. new Ext.grid.GridPanel({  
  14. store:store,  
  15. mode:remote,  

  title:简单Grid表格示例,

  1. applyTo:grid,  
  2. width:250,  
  3. height:150,  
  4. frame:true,  
  5. columns:[  
  6. {header:"id",width:50,dataIndex:id,sortable:true},  
  7. {header:"姓名",width:80,dataIndex:name,sortable:true},  
  8. {header:"年龄",width:80,dataIndex:password,sortable:true}  
  9. ]  
  10. })  
  11. }); 

  

  

  4 运行http://localhost/register/jsonreader.php

  

  5 总结

  php获取MySQL的数据,转换为数组,然后运用json_encode

  

  1. while($row=mysql_fetch_array($result))  
  2. {  
  3. $arr4[]=$row;  
  4. }  
  5. echo json_encode($arr4); 

  以上的相关内容就是对ExtJS与PHP Json、MySQL数据读取的介绍,望你能有所收获  。