Use PHP to call Oracle stored procedures

Source: Internet
Author: User

PHP programs can access the database and use stored procedures. Some people think that the stored procedure is easy to maintain.
However, the wise man sees wisdom. On this issue, I think that using stored procedures means that DBA and developers must work more closely together. If one of them changes, it is obviously difficult to maintain them.
However, using stored procedures has at least two obvious advantages: speed and efficiency.
It is faster to use stored procedures.
In terms of efficiency, if the application needs to perform a series of SQL operations at a time, it needs to migrate to and from PHP and ORACLE. It is better to put 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 used PHP to call the stored procedure and made the following column.

Code :--------------------------------------------------------------------------------

// Create a TEST table
Create table test (
Id number (16) not null,
NAME VARCHAR2 (30) not null,
Primary key (ID)
);

// Insert a data entry
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
)
BEGIN
Select name into p_name
FROM TEST
Where id = 5;
END PROC_TEST;
/

--------------------------------------------------------------------------------

 

PHP code :--------------------------------------------------------------------------------

<? Php

// Establish a 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; // The php variable id to bind.
$ Name = ""; // prepare the php variable name to bind

/** Call the SQL statement of the stored procedure (SQL _sp: SQL _StoreProcedure)
* Syntax:
* BEGIN stored procedure name ([[:] parameter]); END;
* Adding a colon indicates that this parameter is a location
**/
$ 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 location: id, and set the binding length to 16 bits
OCIBindByName ($ stmt, ": name", $ name, 30 );

// Execute
OCIExecute ($ stmt );

// Result
Echo "name is: $ name <br> ";

?>


 

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.