Invoking Oracle stored procedures in PHP

Source: Internet
Author: User
Tags bind php and php code prepare stmt

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
";

?>

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.