How to use Oracle database in PHP (1)

Source: Internet
Author: User
Tags php development environment
How to use the Oracle database in PHP (1), read how to use the Oracle database in PHP (1), php and above, built in almost all the current database processing functions, including Oracle. in this article, we use an instance to introduce how to use these functions to operate Oracle databases. PHP provides two major types of APIs (application interfaces) for Operation "> <LINKhre in php3.0 and later versions, php has built in almost all database processing functions, including Oracle; in this article, we use an instance to introduce how to use these functions to operate Oracle databases.

PHP provides two types of APIs (application interfaces) to operate Oracle databases. One is the standard Oracle processing function (ORA) and the other is the Oracle 8 Call Interface function (OCI8). The latter can only be used in Oracle 7 or 8 versions. Because OCI8 provides many optimization options, the OCI8 interface should be used whenever possible. Here we use these two function sets for demonstration.

First, the premise of this article is that you have installed the Oracle database environment and the PHP development environment. if you do not understand it, it doesn't matter much. There are many good articles on the Internet for reference.

Step 1: create a database for the experiment

You can ask your database administrator or refer to the Oracle User manual for troubleshooting.

Create a data table with ORA

Even if you have created a data table, take a look at the text section. It tells you how to use PHP + SQL technology to operate Oracle

In this example, a data table is created to store personal email addresses.

Related PHP code:


PutEnv ("ORACLE_SID = ORASID ");

$ Connection = Ora_Logon ("username", "password ");
If ($ connection = false ){
Echo Ora_ErrorCode ($ connection). ":". Ora_Error ($ connection )."
";
Exit;
}

$ Cursor = Ora_Open ($ connection );
If ($ cursor = false ){
Echo Ora_ErrorCode ($ connection). ":". Ora_Error ($ connection )."
";
Exit;
}

$ Query = "create table email_info ".
"(Fullname varchar (255), email_address varchar (255 ))";

$ Result = Ora_Parse ($ cursor, $ query );
If ($ result = false ){
Echo Ora_ErrorCode ($ cursor). ":". Ora_Error ($ cursor )."
";
Exit;
}

$ Result = Ora_Exec ($ cursor );
If ($ result = false ){
Echo Ora_ErrorCode ($ cursor). ":". Ora_Error ($ cursor )."
";
Exit;
}

Ora_Commit ($ connection );
Ora_Close ($ cursor );
Ora_Logoff ($ connection );

?>


To process the Oracle database, we must first establish a connection with Oracle.
The syntax is Ora_Logon (user, password). return a connectID ..
Note: Before that, we must set the value of the environment variable ORACLE_SID.

Now, we can perform interactive operations on Oracle using the join ID. The data table name is email_info. The table consists of two domains, one for storing the full name of the individual, (for example: Xiaoyue) one for storing the e-mail address such as (xiaoyue@163.net)

You also need a cursor Ora_Open. This cursor is often used to enumerate data. We use Ora_Parse or Ora_Exec to query the result set of Oracle. Ora_Parse to verify the correctness of the SQL syntax, while Ora_Exec executes the corresponding SQL statement. If all the operations are normal, we will run Ora_Commit to confirm.

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.