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.
Code
Copy code The code is as follows:
//Create a test table
create table test (
id number(16) not null,
name varchar2(30) not null,
primary key (id )
);
//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;
Copy codeThe code is as follows:
//Establish database connection
$user = "scott"; //Database username
$password = "tiger"; // Password
$conn_str = "tnsname"; // Connection string (cstr : connection_string)
$remote = true // Whether to connect remotely
if ($remote) {
$conn = ocilogon($user, $password, $conn_str);
}
else {
$conn = ocilogon($user, $password);
}
//Set binding
$id = 5; // PHP variable id to be bound
$name = ""; // PHP variable name to be bound
/**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
";
?>