Home > Backend Development > PHP Tutorial > Detailed explanation of php calling Oracle's stored procedure

Detailed explanation of php calling Oracle's stored procedure

怪我咯
Release: 2023-03-13 11:30:01
Original
1889 people have browsed it

php program can access the database by using stored procedures. Some people think that using stored procedures is easier to maintain. However, benevolent people have different opinions. On this issue, I think that using stored procedures means that DBA and developers must cooperate more closely. If one of them changes, it will obviously be difficult to maintain.

But using stored procedures has at least two of the most obvious advantages: speed and efficiency. Using stored procedures is obviously faster. In terms of efficiency, if an application needs to perform a series of SQL operations at one time, it needs to go back and forth between PHP and Oracle. It is better to put the application directly into the database to reduce the number of round trips and increase efficiency. But in Internet applications, speed is extremely important, so it is necessary to use stored procedures. I also used PHP to call stored procedures not long ago, and did the following example.

//建立一个TEST表
CREATE TABLE TEST (
  ID        NUMBER(16)        NOT NULL,
  NAME      VARCHAR2(30)      NOT NULL,
  PRIMARY KEY (ID)
);
//插入一条数据
INSERT INTO TEST VALUES (5, 'PHP_BOOK');
//建立一个存储过程
CREATE OR REPLACE PROCEDURE PROC_TEST (
  p_id IN OUT NUMBER,
  p_name OUT VARCHAR2
) AS 
BEGIN
  SELECT NAME INTO p_name
    FROM TEST
    WHERE ID = 5;
END PROC_TEST;
/
Copy after login

PHP code:

<?php
//建立数据库连接
$user = "scott";                //数据库用户名
$password = "tiger";            //密码
$conn_str = "tnsname";          //连接串(cstr : Connection_STRing)
$remote = true                  //是否远程连接
if ($remote) {
  $conn = OCILogon($user, $password, $conn_str);
}
else {
  $conn = OCILogon($user, $password);
}
//设定绑定
$id = 5;                        //准备用以绑定的php变量 id
$name = "";                     //准备用以绑定的php变量 name
/** 调用存储过程的sql语句(sql_sp : SQL_StoreProcedure)
 *  语法:
 *      BEGIN 存储过程名([[:]参数]); END;
 *  加上冒号表示该参数是一个位置
**/
$sql_sp = "BEGIN PROC_TEST(:id, :name); END;";
//Parse
$stmt = OCIParse($conn, $sql_sp);
//执行绑定
OCIBindByName($stmt, ":id", $id, 16);           //参数说明:绑定php变量$id到位置:id,并设定绑定长度16位
OCIBindByName($stmt, ":name", $name, 30);
//Execute
OCIExecute($stmt);
//结果
echo "name is : $name<br>";
?>
Copy after login

The above is the detailed content of Detailed explanation of php calling Oracle's stored procedure. For more information, please follow other related articles on the PHP Chinese website!

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