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
/**SQL statement to call the stored procedure (sql_sp: SQL_StoreProcedure)
* Syntax:
* 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
//Execute
OCIExecute($stmt);
//Result
echo "name is : $name
";
?>