ExtJS与PHP+MYSQL实现数据读取(使用Json)

1 建立数据库、建立表

create database test;
create table test.login(
id int primary key,
name varchar(20) not null,
password varchar(20) not null
);

insert into test.login values
('1','hong','1234'),
('2','linxiang','1234'),
('3','chen','99a9s'),

('4','luxi','aabe2');

 

2 建立jsonreader.php和get.php

jsonreader.php调用json.js

get.php读取数据库数据

jsonreader.php =>

复制代码
< html >
< head >
< title > 注册 </ title >
    
< link  rel ="stylesheet"  type ="text/css"  href ="../ext/resources/css/ext-all.css"   />
    
< script  type ="text/javascript"  src ="../ext/adapter/ext/ext-base.js" ></ script >
    
< script  type ="text/javascript"  src ="../ext/ext-all.js" ></ script >
    
< script  type ="text/javascript"  src ="json.js" ></ script >
</ head >
< body >
    
< div  id ='grid' ></ div >
</ body >
</ html >
复制代码

get.php=>

复制代码
  <? php
    
$conn = mysql_connect ( " localhost " , " root " , " 123 " );
    
mysql_select_db ( " test " );
    
$sql = " select id,name,password from login " ;
    
$result = mysql_query ( $sql , $conn );
    
    
while ( $row = mysql_fetch_array ( $result ))
    {
        
$arr4 [] = $row ;
    }
    
echo  json_encode( $arr4 );
?>
复制代码

3 extjs文件json.js编写

json.js=>

复制代码
Ext.onReady( function () {
    store
= new  Ext.data.JsonStore({
        url:
' get.php ' ,
        data:[],
        fields:[
            {name:
' id ' },
            {name:
' name ' },
            {name:
' password ' }
        ]
    });
    store.load();
    
new  Ext.grid.GridPanel({
        store:store,
        mode:
' remote ' ,
        title:
' 简单Grid表格示例 ' ,
        applyTo:
' grid ' ,
        width:
250 ,
        height:
150 ,
        frame:
true ,
        columns:[
            {header:
" id " ,width: 50 ,dataIndex: ' id ' ,sortable: true },
            {header:
" 姓名 " ,width: 80 ,dataIndex: ' name ' ,sortable: true },
            {header:
" 年龄 " ,width: 80 ,dataIndex: ' password ' ,sortable: true }
        ]
    })
});
复制代码


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

 

5 总结

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

    while ( $row = mysql_fetch_array ( $result ))
    {
        
$arr4 [] = $row ;
    }
    
echo  json_encode( $arr4 );

转载于:https://my.oschina.net/junn/blog/90671

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值