Home  >  Article  >  Backend Development  >  Calling Oracle stored procedures with PHP_PHP tutorial

Calling Oracle stored procedures with PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:07:59996browse

PHP programs can use stored procedures to access databases. Some people think that using stored procedures is easier to maintain.
But different people have different opinions. On this issue, I think using stored procedures means that DBA and developers must be more careful. Tight coordination, 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, and I did the following example.

Code:--------------------------------------------- -----------------------------------------------

//Create a TEST table
CREATE TABLE TEST (
ID NUMBER(16) NOT NULL,
NAME VARCHAR2(30) NOT NULL,
PRIMARY KEY (ID)
);

// Insert a piece of data
INSERT INTO TEST VALUES (5, 'PHP_BOOK');

//Create a stored procedure
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;
/

-- -------------------------------------------------- ----------------------------

PHP code:------- -------------------------------------------------- -----------------------

//Establish database connection
$user = "scott";                    // Database username
$password = "tiger";                                                                                                                                              . Whether to connect remotely
if ($remote) {
$conn = OCILogon($user, $password, $conn_str);
}
else {
$conn = OCILogon($user, $password);
}

//Set binding

$id = 5; Prepare the php variable name for binding


/**SQL statement to call the stored procedure (sql_sp: SQL_StoreProcedure)
* Syntax:

* BEGIN stored procedure name ([[:] parameter]); END;

* Adding a colon indicates that the parameter is a position
**/
$sql_sp = "BEGIN PROC_TEST(:id, :name); END;";

//Parse
$stmt = OCIParse($conn, $sql_sp);

//Execute binding
OCIBindByName($stmt, ":id", $id, 16); // Parameter description: Bind the php variable $id to the position:id, and set the binding length to 16 bits

OCIBindByName($stmt, ":name", $name, 30);


//Execute
OCIExecute($stmt);

//Result
echo "name is : $name
";

?>


http://www.bkjia.com/PHPjc/314948.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314948.htmlTechArticlePHP program can use stored procedures to access the database. Some people think that using stored procedures is easier to maintain. , on this issue, I think using storage...
Statement:
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