background image

用 php 调用数据库的存贮过程!
作者:fox4000 

昨天,看到一个战友问是否可以用 php 调用存贮过程,感觉应该是可以的,所以,马上

 

进行了实验,非常的成功!非常出乎我的意料之外!因此,写出来,给大家参考!
大家知道,存储过程是在服务器端的一个脚本程序,执行起来速度很快,但它也有一个

 

缺点,就是依赖与一个固定数据库,移植性不好!
我的上回文章,提到了用 com 组件是可以访问 ado 以及相关的组件,无论是自己建的还
是系统带的,都可以扩展系统的功能,但现在 php 不支持 dcom/com+,但相信它的下一个

 

版本应该是支持的。

 

不说这么多了,我们马上试一下吧。

 

下面是我的一个简单的存贮过程
CREATE PROCEDURE [sp_mystoreprocedure] AS 
select companyname, contactname, city from customers 

 

其实,还可以写比较复杂的,可惜我对此研究不深,只好取简单了!

下面是我的 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;