天天看點

用PHP調用資料庫的存貯過程

本文為大家詳細介紹用php調用資料庫的存貯過程的相關内容。

昨天,看到一個戰友問是否可以用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;

print '</b><br></td>

<td align="middle">';

print $fld1->value;

print '</td><td align="middle">';

print $fld2->value;

print '</td></tr>';

$rs->movenext();

}

print '</table>';

$rs->close();

?>

注意的是,你的伺服器必須打開!另外,就是不能寫錯存貯過程的名稱。否則會出項緻命的錯誤,而且,你根本就不知道錯誤在那裡,這就是php檔案對錯誤處理的不好之處,但相信它以後是會改進的。

我學php需然有很長時間了,但發現要真正用好它,不那麼容易,但它确實也超出了我的想象,有些東西真的很奇妙,真是,不用不知道,一用真奇妙!

http://www.yc-edu.org/phppeixun/3729.html