Use PHP to call the stored procedure of the database!

WBOY
Release: 2016-08-08 09:33:51
Original
1837 people have browsed it

Use php to call the stored procedure of the database!
Author: fox4000

Yesterday, I saw a comrade asking if the stored procedure could be called using PHP. I felt that it should be possible, so I immediately conducted the experiment and it was very successful! Very unexpected! Therefore, write it down for everyone’s reference!
As we all know, a stored procedure is a script program on the server side, which is very fast to execute, but it also has a disadvantage, that is, it relies on a fixed database and has poor portability!
In my last article, I mentioned that you can use COM components to access ADO and related components. Whether you build it yourself or come with the system, you can expand the functions of the system. But now PHP does not support dcom/com+, but I believe it can The next version should support it.
Without further ado, let’s try it right away.

Below is a simple storage process of mine
CREATE PROCEDURE [sp_mystoreprocedure] AS
select companyname, contactname, city from customers

In fact, I could write something more complicated, but unfortunately I didn’t study it deeply, so I had to keep it simple!

Below is my php file
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 '






';

while (!$rs->EOF) {
$i += 1;
$fld0 = $rs->Fields(0);
$fld1 = $rs->Fields(1);
$fld2 = $rs->Fields(2);
print '

';

$rs->MoveNext();
}
print '
Directive Local Value Master Value
';
print $fld0->value;
print '

';
print $fld1->value;
print '
';
print $fld2->value;
print '
';

$rs->Close();
?>

Note that your server must be turned on! In addition, you cannot write the wrong name of the stored procedure. Otherwise, a fatal error will occur, and you don't even know where the error is. This is the problem with error handling in the PHP file, but I believe it will be improved in the future.


The above introduces the stored procedure of calling the database using PHP! , including the content of calling stored procedures. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!