Teach you how to invoke Oracle stored procedures in PHP:
PHP Program Access to the database, you can use stored procedures, some people think that the use of stored procedures to facilitate maintenance
However, in this case, I think that using stored procedures means that DBAs and developers have to work more closely together, which is obviously difficult to maintain if one party accounts.
But using stored procedures has at least two of the most obvious advantages: speed and efficiency.
The speed of using stored procedures is obviously faster.
In efficiency, if you need to do a series of SQL operations, you need to go back and forth between PHP and Oracle, it is better to put the application directly to the database side to reduce the number of round-trip, increase efficiency.
However, in Internet applications, speed is extremely important, so it is necessary to use stored procedures.
I also use PHP to invoke the stored procedure soon, do the following.
Code:--------------------------------------------------------------------------------
Create a test table
CREATE TABLE TEST (
ID number is not NULL,
NAME VARCHAR2 not NULL,
PRIMARY KEY (ID)
);
Insert a piece of data
INSERT into TEST VALUES (5, ' Php_book ');
Establish 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:--------------------------------------------------------------------------------
Establishing a database connection
$user = "Scott"; Database user Name
$password = "Tiger"; Password
$conn _str = "Tnsname"; Connection string (cstr:connection_string)
$remote = TRUE//Whether remote connection
if ($remote) {
$conn = Ocilogon ($user, $password, $conn _str);
}
else {
$conn = Ocilogon ($user, $password);
}
Set binding
$id = 5; Prepare the PHP variable ID to bind to
$name = ""; Prepare the PHP variable name to bind to
/** the SQL statement that invokes the stored procedure (sql_sp:sql_storeprocedure)
Syntax
* BEGIN Stored procedure name ([[:] parameter]); End;
* Plus a colon to indicate that the parameter is a position
**/
$sql _sp = "BEGIN proc_test (: ID,: name); end; ";
Parse
$stmt = Ociparse ($conn, $sql _sp);
Performing bindings
Ocibindbyname ($stmt, ": id", $id, 16); Parameter description: Bind PHP variable $id to position: ID, and set binding length 16 bits
Ocibindbyname ($stmt, ": Name", $name, 30);
Execute
Ociexecute ($stmt);
Results
echo "Name is: $name
";
?>