PHP Program Access to the database, fully use stored procedures, some people think that the use of stored procedures for easy maintenance
But, benevolent see, on this issue, I think the use of stored procedures means that DBAs and developers need to work more closely together, and if one of them is notconsistent, it is obviously difficult to maintain.
But there are at least two of the most obvious advantages of using stored procedures: speed and efficiency.
The speed of using stored procedures is obviously faster.
In efficiency, if the application needs to do a series of SQL operations at a time, you need to go to PHP and Oracle, instead of putting the application directly to the database side to reduce the number of round trips and increase efficiency.
However, in Internet applications, speed is extremely important, so it is necessary to use stored procedures.
I also use PHP to call the stored procedure shortly, to do the following as a dummy.
Code:--------------------------------------------------------------------------------
Create a test table
CREATE TABLE TEST (
ID number (+) is not NULL,
NAME VARCHAR2 (+) is 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 bindings
$id = 5; Prepare the PHP variable ID to be used for binding
$name = ""; Prepare the PHP variable for binding name
/** SQL statement that invokes a stored procedure (sql_sp:sql_storeprocedure)
Syntax
* BEGIN Stored procedure name ([[:] parameter]); END;
* Plus a colon indicates 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 location: ID, and set binding length 16 bits
Ocibindbyname ($stmt, ": Name", $name, 30);
Execute
Ociexecute ($stmt);
Results
echo "Name is: $name
";
?>
http://www.bkjia.com/PHPjc/314948.html www.bkjia.com true http://www.bkjia.com/PHPjc/314948.html techarticle PHP Program Access to the database, can fully use stored procedures, some people think that the use of stored procedures for ease of maintenance but the benevolent see, in this issue, I think the use of stored over ...