Using PHP to invoke Oracle stored procedures

Source: Internet
Author: User
Tags bind learn php php and php code prepare stmt
oracle| Stored Procedures

From the beginning to learn PHP to Joy International village, for several months, learned a lot of things, I wrote an example hope a little useful.

The PHP program accesses the database and can use the stored procedure completely.
Some people think that using stored procedures facilitates maintenance
However, in this issue, I think that using stored procedures means that DBAs and developers need to work more closely together
If one party accounts, it is obviously difficult to maintain.
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.

This is my first time to write articles (formerly are Fashui stickers), I hope that many treatise!

Code:
-------------------------------------------------------------
Create a test table CREATE TABLE test (ID number () NOT NULL, NAME VARCHAR2 (a) not NULL, PRIMARY KEY (id)); Inserts a 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_n Ame 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
";

?>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.