PHP links to Oracle PHP to call Oracle stored procedures

Source: Internet
Author: User

PHP calls the Oracle row Stored Procedure ################################## ##########################

PHP programs can access the database and use stored procedures. Some people think that the stored procedure is easy to maintain.
However, the benevolent sees benevolence, and the wise sees wisdom.
On this issue, I think that using stored procedures means that dBA and developers must work 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.

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

?>
######################################## ####################

PHP links to Oracle ##################################### #######################

<? PHP
// The third item uses Oracle easy connect string. For more information, see the http://download.oracle.com/docs/cd/E11882_01/network.112/e10836/naming.htm#NETAG255.
$ Conn = oci_connect ("username", "password", "url/SERVICE_NAME ");

$ SQL = "select * from (select rownum R, ima_file. * From ima_file) s where S. R> = 9000 and S. r <= 9020 ";
$ Stmt = oci_parse ($ Conn, $ SQL );

// Oci_default means that any change requires commit or rollback.
Oci_execute ($ stmt, oci_default );
$ I = 0;
Echo "<Table border = 1> <caption> ima_file </caption> <thead> <TD> count </TD> <TD> Number of rows </TD> <TD> item number </TD> <TD> product name </TD> <TD> specification </TD> </thead> ";
While ($ ROW = oci_fetch_array ($ stmt, oci_both )){
// Oci_both creates an array that can use both a numeric index and a field name to point to the same item.
Echo "<tr> <TD> ". ++ $ I. "</TD> <TD> $ row [0] </TD> <TD >{$ row [1]} </TD> <TD >{$ row [2]} </TD> <TD> ";
/* This column may have a null value.
* According to the OCI mechanism, null values are not read and skipped directly. For example
* If the value is null, This item does not exist in the array, but jumps from 2 to 4.
* (When the fourth value is not null). Therefore, no matter the index
* 3 or 'ima021 'does not exist. An error is reported when you directly reference it. Therefore, you must first
* Perform a row test and then reference it.
*/
If (array_key_exists ("ima021", $ row )){
Echo $ row ["ima021"];
}
Echo "</TD> </tr> ";
}
Echo "</table> ";
Oci_cancel ($ stmt); // not required. This is used as a demo
Oci_close ($ conn );
?>

Here is the above example of csdn
Php oci connection to Oracle

1. Output phpinfo to check whether OCI support is installed in PHP and check whether OCI is loaded in PHP. ini.
2. If the remote server is connected, you must install the Oracle client on the Web server. Otherwise, you cannot link to Oracle in the following way.
3. Connection method:
A)
$ CDN = "(description = (address = (Protocol = TCP) (host = 192.168.10.xxx) (Port = port) (CONNECT_DATA = (SID = Database Name )))";
$ Con1 = oci_connect ('username', 'Password', $ CDN) or die ("connection failed ");

B) $ con2 = ocilogon ("username", ", password", "// 192.168.10.xxx: Port/Database Name") or die ("connection failed ");

Note: Both ocilogon () and oci_connect () can be used.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/ht8326/archive/2009/01/07/3725800.aspx
######################################## ####################

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.