ajaxtoday 发表于 2006-12-30 16:37

[转载]AJAX for PHP简单表数据查询实例

<P  align=center><B ><FONT face="Times New Roman">AJAX for PHP</FONT></B><B >简单表数据查询实例</B><B ><o:p></o:p></B></P>
<P >功能介绍:<FONT face="Times New Roman">AJAX WebShop 3</FONT>从<FONT face="Times New Roman">Beta2</FONT>开始支持<FONT face="Times New Roman">PHP</FONT>的开发了,<FONT face="Times New Roman">AJAX WebShop</FONT>集成了<FONT face="Times New Roman">PHP5</FONT>的开发环境,因此不需要额外安装配置<FONT face="Times New Roman">PHP</FONT>,本例将实现一个<FONT face="Times New Roman">AJAX for PHP</FONT>的简单数据查询操作,这个例子是单表操作,也可以实现主从表的数据查询。</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><B >一<FONT face="Times New Roman">.</FONT></B><B >数据表说明<o:p></o:p></B></P>
<P >例子采用了<FONT face="Times New Roman">Access</FONT>数据库,当然你也可以使用<FONT face="Times New Roman">mysql </FONT>或其他类型数据库,数据库名称为:<FONT face="Times New Roman">demo.mdb</FONT>,表名为<FONT face="Times New Roman">product</FONT>,创建字段分别是<FONT face="Times New Roman">PRODUCT_ID, PRODUCT_NAME, PRODUCT_PRICE, PRODUCT_AREA</FONT>。</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><B >二<FONT face="Times New Roman">.</FONT></B><B >实现数据查询<o:p></o:p></B></P>
<P >首先启动<FONT face="Times New Roman">AJAX WebShop 3</FONT>,在<FONT face="Times New Roman">File</FONT>中选择“<FONT face="Times New Roman">New Project</FONT>”建立新工程。</P>
<P ><v:shapetype><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype>[attach]15393[/attach]</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P >在弹出的<FONT face="Times New Roman">New Project</FONT>对话框中设置<FONT face="Times New Roman">projectname </FONT>和<FONT face="Times New Roman">web server</FONT>本例中设置<FONT face="Times New Roman">projectname</FONT>为:<FONT face="Times New Roman">php_example</FONT>和设置<FONT face="Times New Roman">web server</FONT>为:<FONT face="Times New Roman">PHP</FONT>。如果要修改工程路径,请在<FONT face="Times New Roman">Directory</FONT>中设置要存放的路径。</P>
<P >[attach]15394[/attach]</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P >设置好<FONT face="Times New Roman">New Project</FONT>后,再打开“<FONT face="Times New Roman">File</FONT>”选择“<FONT face="Times New Roman">New .PHP Service</FONT>”,在弹出的对话框内目录输入子目录<FONT face="Times New Roman">demo</FONT>,设置<FONT face="Times New Roman">”Class Name”</FONT>为:<FONT face="Times New Roman">simple_query</FONT>;在<FONT face="Times New Roman">ServiceType</FONT>中选择“<FONT face="Times New Roman">Query Data</FONT>”点击“<FONT face="Times New Roman">OK</FONT>“后,向导将按默认模板生成<FONT face="Times New Roman">php</FONT>单表查询的代码。</P>
<P >[attach]15395[/attach]</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P >输入以下数据库连接及<FONT face="Times New Roman">sql</FONT>代码<FONT face="Times New Roman">(demo.mdb</FONT>文件放在当前代码目录下<FONT face="Times New Roman">)</FONT>:</P>
<TABLE  cellSpacing=0 cellPadding=0 border=1>

<TR >
<TD  vAlign=top width=568>
<P ><FONT face="Times New Roman">$connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=".realpath($_SERVER['PATH_TRANSLATED'])."\demo.mdb";</FONT></P>
<P ><FONT face="Times New Roman">$sql     = "select * from product";</FONT></P>
<P ><FONT face="Times New Roman">$sqlcount     =  "select count(*) from product";</FONT></P></TD></TR></TABLE>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><FONT face="Times New Roman">  </FONT>分析<FONT face="Times New Roman">testquery.php</FONT>代码,主要的实现如下:</P>
<P ><FONT face="Times New Roman">1.       </FONT>循环数据结构,用<FONT face="Times New Roman">addField</FONT>向<FONT face="Times New Roman">$xmlRequest</FONT>填充字段(列):</P>
<TABLE  cellSpacing=0 cellPadding=0 border=1>

<TR >
<TD  vAlign=top width=568>
<P ><FONT face="Times New Roman">              //fill metadata fields</FONT></P>
<P ><FONT face="Times New Roman">        for ($i=1; $i&lt;=$fields_count; $i++) {   </FONT></P>
<P ><FONT face="Times New Roman">            $fieldname = odbc_field_name($result_id, $i);   </FONT></P>
<P ><FONT face="Times New Roman">            $datatype  = odbc_field_type($result_id, $i); </FONT></P>
<P ><FONT face="Times New Roman">            $xmlRequest-&gt;addField($fieldname, $datatype);  </FONT></P>
<P ><FONT face="Times New Roman">        }</FONT></P></TD></TR></TABLE>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><FONT face="Times New Roman">2. </FONT>循环结果集记录,调用<FONT face="Times New Roman">Append() </FONT>,<FONT face="Times New Roman">SetValue</FONT>方法,向<FONT face="Times New Roman">xmlrequest</FONT>每一行对应字段填充数据:</P>
<TABLE  cellSpacing=0 cellPadding=0 border=1>

<TR >
<TD  vAlign=top width=568>
<P ><FONT face="Times New Roman">        //fill data rows</FONT></P>
<P ><FONT face="Times New Roman">        for ($i=0; $i&lt;$record_count; $i++) {</FONT></P>
<P ><FONT face="Times New Roman">                     odbc_fetch_row($result_id);</FONT></P>
<P ><FONT face="Times New Roman">                     if($i&gt;=$recNo &amp;&amp; $i&lt;$recNo+$maxRows) {</FONT></P>
<P ><FONT face="Times New Roman">                            $xmlRequest-&gt;append();</FONT></P>
<P ><FONT face="Times New Roman">                      for ($j=1; $j&lt;=$fields_count; $j++) {   </FONT></P>
<P ><FONT face="Times New Roman">                                   $xmlRequest-&gt;setValueByIndex($j-1, odbc_result($result_id, $j));</FONT></P>
<P ><FONT face="Times New Roman">                            }</FONT></P>
<P ><FONT face="Times New Roman">                     }</FONT></P>
<P ><FONT face="Times New Roman">                     if($i&gt;=$recNo+$maxRows) break;</FONT></P>
<P ><FONT face="Times New Roman">              }</FONT></P></TD></TR></TABLE>
<P ><FONT face="Times New Roman">3.  </FONT>设置分页的相关参数,<FONT face="Times New Roman">$xmlRequest-&gt;recNo</FONT>是开始记录,<FONT face="Times New Roman">$xmlRequest-&gt;maxRows</FONT>是每页记录数,通过执行<FONT face="Times New Roman">"select count(*) from product"</FONT>得到<FONT face="Times New Roman">$record_count</FONT>记录总数:</P>
<TABLE  cellSpacing=0 cellPadding=0 border=1>

<TR >
<TD  vAlign=top width=568>
<P ><FONT face="Times New Roman">              $sqlcount     =  "select count(*) from product";</FONT></P>
<P ><FONT face="Times New Roman">              $result_id    = @odbc_do($connid, $sqlcount);</FONT></P>
<P ><FONT face="Times New Roman">        if($result_id==null)</FONT></P>
<P ><FONT face="Times New Roman">           throw new Exception($sqlcount);</FONT></P>
<P ><FONT face="Times New Roman">              odbc_fetch_row($result_id);</FONT></P>
<P ><FONT face="Times New Roman">              $record_count =  odbc_result($result_id,1);     </FONT></P>
<P ><FONT face="Times New Roman">              $xmlRequest-&gt;setRecordCount($record_count); </FONT></P>
<P ><FONT face="Times New Roman">              $recNo        = $xmlRequest-&gt;recNo;</FONT></P>
<P ><FONT face="Times New Roman">              $maxRows      = $xmlRequest-&gt;maxRows;</FONT></P>
<P ><FONT face="Times New Roman">              if($maxRows==-1) $maxRows = $record_count;</FONT></P></TD></TR></TABLE>
<P >后台数据访问类建立好后,在“<FONT face="Times New Roman">File</FONT>”中选择“<FONT face="Times New Roman">New Page</FONT>”打开“<FONT face="Times New Roman">New Page</FONT>”对话框在“<FONT face="Times New Roman">File Name</FONT>”中设置页面名称,如本例“<FONT face="Times New Roman">simple.htm</FONT>”点击<FONT face="Times New Roman">ok</FONT>完成设置。</P>
<P >[attach]15396[/attach]<BR></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><FONT face="Times New Roman">   </FONT>在<FONT face="Times New Roman">webshop</FONT>的元件选项板“<FONT face="Times New Roman">Data Access </FONT>”,“<FONT face="Times New Roman">DBControl</FONT>”中,拖拉向表单加入<FONT face="Times New Roman">DataSet</FONT>和数据感应控件<FONT face="Times New Roman">DBNavigator</FONT>、<FONT face="Times New Roman">DBEdit</FONT>、<FONT face="Times New Roman">DBGrid</FONT>等,简单地可视化拖拉建立界面,点击组件,会在右侧出现相应属性。</P>
<P >选择<FONT face="Times New Roman">DataSet1</FONT>,在“<FONT face="Times New Roman">OpenURL</FONT>”属性中添加<FONT face="Times New Roman">web service</FONT>名“<FONT face="Times New Roman">demo/simple_query.php</FONT>”。</P>
<P >打开<FONT face="Times New Roman">WebShop</FONT>中的“<FONT face="Times New Roman">Run</FONT>”启动<FONT face="Times New Roman">”Start WebServer”</FONT>服务,<FONT face="Times New Roman">Apache HttpServer</FONT>将启动。</P>
<P >成功启动<FONT face="Times New Roman"> </FONT>“<FONT face="Times New Roman">WebServer”</FONT>服务后,在<FONT face="Times New Roman">DataSet1</FONT>的“<FONT face="Times New Roman">Fields”</FONT>属性中添加数据字段,方法为点击右侧“<FONT face="Times New Roman">…</FONT>”弹出<FONT face="Times New Roman">Fields</FONT>对话框,在页面单击右键,在弹出选项中选择“<FONT face="Times New Roman">Add Field</FONT>”在弹出的“<FONT face="Times New Roman">Field List”</FONT>中点击<FONT face="Times New Roman"> “Refresh”</FONT>会在列表中显示数据表<FONT face="Times New Roman"> </FONT>“<FONT face="Times New Roman">Product </FONT>”的字段名,全部选中后,点击“<FONT face="Times New Roman">ok</FONT>”完成设置(操作见下图)。</P>
<P >[attach]15397[/attach]</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P >设置好数据连接后,“<FONT face="Times New Roman">DataSet1</FONT>的“<FONT face="Times New Roman">Active”</FONT>属性设置为“<FONT face="Times New Roman">true</FONT>”如图;</P>
<P >  同时选择<FONT face="Times New Roman">DBGrid1</FONT>、<FONT face="Times New Roman">DBNavigator</FONT>、<FONT face="Times New Roman">DBEdit</FONT>等界面对象,在<FONT face="Times New Roman">DataSet</FONT>属性中选择数据源 “<FONT face="Times New Roman">DataSet1”</FONT>。完成数据库连接;另外,<FONT face="Times New Roman">DBEdit</FONT>需要选择设置<FONT face="Times New Roman">DataField</FONT>属性,指定要绑定的字段,点击“<FONT face="Times New Roman">run</FONT>”查看运行效果。</P>
<P >[attach]15398[/attach]</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P >还有很多功能可以很简单的实现,这里就不一一介绍了,以后有时间我再说说如何做数据提交、分页及更复杂的主从表等数据操作。</P>
<P >下面附上详细的代码清单,供读者参考。</P>
<TABLE  cellSpacing=0 cellPadding=0 border=1>

<TR >
<TD  vAlign=top width=568>
<P ><FONT face="Times New Roman">&lt;?php</FONT></P>
<P ><FONT face="Times New Roman">/**</FONT></P>
<P ><FONT face="Times New Roman"> * Title: </FONT></P>
<P ><FONT face="Times New Roman"> * Description: </FONT></P>
<P ><FONT face="Times New Roman"> * Copyright: Copyright (c) 2006</FONT></P>
<P ><FONT face="Times New Roman"> *</FONT></P>
<P ><FONT face="Times New Roman"> * $Author:</FONT></P>
<P ><FONT face="Times New Roman"> * $Date:</FONT></P>
<P ><FONT face="Times New Roman"> * $Revision:</FONT></P>
<P ><FONT face="Times New Roman"> */</FONT></P>
<P ><FONT face="Times New Roman">       header("Content-Type:text/xml");</FONT></P>
<P ><FONT face="Times New Roman">       require_once('../joyistar/XmlRequest.php');</FONT></P>
<P ><FONT face="Times New Roman">       $postdata   = file_get_contents("php://input");</FONT></P>
<P ><FONT face="Times New Roman">       $xmlRequest = new XmlRequest();</FONT></P>
<P ><FONT face="Times New Roman">       $xmlRequest-&gt;open($postdata);</FONT></P>
<P ><FONT face="Times New Roman">       try {</FONT></P>
<P ><FONT face="Times New Roman">              $connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=".realpath($_SERVER['PATH_TRANSLATED'])."\demo.mdb";</FONT></P>
<P ><FONT face="Times New Roman">              $sql     = "select * from product";</FONT></P>
<P ><FONT face="Times New Roman">              $connid  = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die ("db connect error!");</FONT></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><FONT face="Times New Roman">              $sqlcount     =  "select count(*) from product";</FONT></P>
<P ><FONT face="Times New Roman">              $result_id    = @odbc_do($connid, $sqlcount);</FONT></P>
<P ><FONT face="Times New Roman">        if($result_id==null)</FONT></P>
<P ><FONT face="Times New Roman">           throw new Exception($sqlcount);</FONT></P>
<P ><FONT face="Times New Roman">              odbc_fetch_row($result_id);</FONT></P>
<P ><FONT face="Times New Roman">              $record_count =  odbc_result($result_id,1);     </FONT></P>
<P ><FONT face="Times New Roman">              $xmlRequest-&gt;setRecordCount($record_count); </FONT></P>
<P ><FONT face="Times New Roman">              $recNo        = $xmlRequest-&gt;recNo;</FONT></P>
<P ><FONT face="Times New Roman">              $maxRows      = $xmlRequest-&gt;maxRows;</FONT></P>
<P ><FONT face="Times New Roman">              if($maxRows==-1) $maxRows = $record_count;</FONT></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><FONT face="Times New Roman">              $result_id   = @odbc_do($connid, $sql);</FONT></P>
<P ><FONT face="Times New Roman">        if($result_id==null)</FONT></P>
<P ><FONT face="Times New Roman">           throw new Exception($sql);</FONT></P>
<P ><FONT face="Times New Roman">              $fields_count = odbc_num_fields($result_id);   </FONT></P>
<P ><FONT face="Times New Roman">              //fill metadata fields</FONT></P>
<P ><FONT face="Times New Roman">        for ($i=1; $i&lt;=$fields_count; $i++) {   </FONT></P>
<P ><FONT face="Times New Roman">            $fieldname = odbc_field_name($result_id, $i);   </FONT></P>
<P ><FONT face="Times New Roman">            $datatype  = odbc_field_type($result_id, $i); </FONT></P>
<P ><FONT face="Times New Roman">            $xmlRequest-&gt;addField($fieldname, $datatype);  </FONT></P>
<P ><FONT face="Times New Roman">        }           </FONT></P>
<P ><FONT face="Times New Roman">        //fill data rows</FONT></P>
<P ><FONT face="Times New Roman">        for ($i=0; $i&lt;$record_count; $i++) {</FONT></P>
<P ><FONT face="Times New Roman">                     odbc_fetch_row($result_id);</FONT></P>
<P ><FONT face="Times New Roman">                     if($i&gt;=$recNo &amp;&amp; $i&lt;$recNo+$maxRows) {</FONT></P>
<P ><FONT face="Times New Roman">                            $xmlRequest-&gt;append();</FONT></P>
<P ><FONT face="Times New Roman">                      for ($j=1; $j&lt;=$fields_count; $j++) {   </FONT></P>
<P ><FONT face="Times New Roman">                                   $xmlRequest-&gt;setValueByIndex($j-1, odbc_result($result_id, $j));</FONT></P>
<P ><FONT face="Times New Roman">                            }</FONT></P>
<P ><FONT face="Times New Roman">                     }</FONT></P>
<P ><FONT face="Times New Roman">                     if($i&gt;=$recNo+$maxRows) break;</FONT></P>
<P ><FONT face="Times New Roman">              }</FONT></P>
<P ><FONT face="Times New Roman">              odbc_close($connid);</FONT></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><FONT face="Times New Roman">             echo $xmlRequest-&gt;getXml();</FONT></P>
<P ><FONT face="Times New Roman">       }</FONT></P>
<P ><FONT face="Times New Roman">       catch (Exception $e) {</FONT></P>
<P ><FONT face="Times New Roman">              $xmlRequest-&gt;setError($e-&gt;getTraceAsString());</FONT></P>
<P ><FONT face="Times New Roman">             echo $xmlRequest-&gt;getXml();</FONT></P>
<P ><FONT face="Times New Roman">              throw $e;</FONT></P>
<P ><FONT face="Times New Roman">       } </FONT></P>
<P ><FONT face="Times New Roman">?&gt;</FONT></P></TD></TR></TABLE>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P><BR>原文转自<a href="http://cn.joyistar.com" target="_blank" >http://cn.joyistar.com</A>

kyoshingo 发表于 2007-1-9 15:39

这东西在哪下啊

kyoshingo 发表于 2007-1-9 16:13

软件我有了,有没有他的注册码啊?

ajaxtoday 发表于 2007-2-5 12:11

不用注册码也可以用的,我就没用注册的,只是按照他们的产品协议,用作商业开发的要收费的,自己开发是免费的!!!

页: [1]

编程论坛