用PHP调用数据库的存贮过程
大家知道,存储过程是在服务器端的一个脚本程序,执行起来速度很快,但它也有一个缺点,就是依靠与一个固定数据库,移植性不好.
我的上回文章,提到了用com组件是可以访问ado以及相关的组件,无论是自己建的还是系统带的,都可以扩展系统的功能,但现在php不支持dcom/com,但相信它的下一个版本应该是支持的.
不说这么多了,我们马上试一下吧,下面是我的一个简单的存贮过程:
CREATE PROCEDURE [sp_mystoreprocedure] AS
select companyname,contactname,city from customers
其实,还可以写比较复杂的,可惜我对此研究不深,只好取简单了,下面是我的php文件:
<?php define ( "OLEDB_CONNECTION_STRING" , "Provider=SQLOLEDB; Data Source=zzb; Initial Catalog=Northwind; User ID=sa; Password=" ); $dbc = new COM( "ADODB.Connection" ); $dbc->Open(OLEDB_CONNECTION_STRING); $command = "sp_mystoreprocedure" ; $rs = $dbc->Execute($command); // Recordset $i = 0; echo '<table cellSpacing= "1" cellPadding= "3" width= "600" align= "center" bgColor= "#000000" border= "0" > <tr vAlign= "bottom" bgColor= "#9999cc" > <th>Directive</th> <th>Local Value</th> <th>Master Value</th> </tr>'; while (!$rs->EOF) { $i = 1; $fld0 = $rs->Fields(0); $fld1 = $rs->Fields(1); $fld2 = $rs->Fields(2); print '<tr vAlign= "baseline" bgColor= "#cccccc" > <td bgColor= "#ccccff" ><b>'; print $fld0->value; print '</b><br></td> <td align= "middle" >'; print $fld1->value; print '</td><td align="middle">' ; print $fld2->value; print '</td></tr>' ; //开源代码phpfensi测试数据 $rs->MoveNext(); } print '</TABLE>' ; $rs->Close(); ?>注重的是,你的服务器必须打开,另外,就是不能写错存贮过程的名称,否则会出项致命的错误,而且,你根本就不知道错误在那里,这就是php文件对错误处理的不好之处,但相信它以后是会改进的.
我学php需然有很长时间了,但发现要真正用好它,不那么轻易,但它确实也超出了我的想象,有些东西真的很奇妙,真是,不用不知道.一用真奇妙.
查看更多关于用PHP调用数据库的存贮过程 - php高级应用的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did30428