下面总结了三种流行的数据库,如何利用php来调用它们的存储过程,我们讲有mysql教程,oracle,mssql server.
PHP实例代码如下:
- <?php
- function check_login($user, $pass) {
- $conn = ocilogon('user', 'pass', 'database');
- $sql = 'begin :result := test.check_login(:user, :pass); end;';
- $stmt = oci_parse($conn, $sql);
-
- $result = '';
- oci_bind_by_name($stmt, ':user', $user, 32);
- oci_bind_by_name($stmt, ':pass', md5($pass), 32);
- oci_bind_by_name($stmt, ':result', $result, 10);
- oci_execute($stmt);
- ocilogoff($conn);
-
- return $result;
- }
- ?>
调用mysql,存储过程改成:
- create procedure in_out(in uid int) begin
- set @msg='hello';
- select *,@msg from manage_loginhistory where h_uid=uid;
- end;
php调用改成:
- $sql = "call in_out(39)";
- $rs=mysql_query($sql);
- $row=mysql_fetch_array($rs);
调用ms sql server:
- $user_name = '龙之泪';
- $password = '123456';
- $info = '';
- $host="192.168.0.1";
- $user="sa";
- $password="123456";
- $db="sample";
- $dblink=mssql_connect($host,$user,$password) or die("can't connect to mssql");
- mssql_select_db($db,$dblink) or die("can't select sample");
-
- $sp = mssql_init("test");
-
-
- mssql_bind($sp,"@user_name",$user_name,sqlvarchar,false,false,30);
- mssql_bind($sp,"@password",$password,sqlvarchar,false,false,30);
- mssql_bind($sp,"@info",$info,sqlvarchar,true,false,30);
- mssql_execute($sp);
-
- echo $info;
|