How to use Oracle Database in PHP (1) _php Basics

Source: Internet
Author: User
Tags php development environment
In php3.0 above, PHP has nearly all of the current database processing functions, including Oracle; In this article we have an example of how to use these functions to manipulate Oracle databases.

PHP provides 2 classes 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 on Oracle 7 or 8 versions. Because OCI8 offers a number of optimization options, the OCI8 interface should be used whenever possible. Here we demonstrate each of these two function sets.

First of all this article assumes that you have installed the Oracle database environment and the PHP development environment. If you do not know how much relationship, there are a lot of relevant good articles on the internet can be referenced.

First step: Create an experimental database

This problem you can ask your database administrator or refer to the Oracle User manual processing, here no more talk

Set up data tables with ORA

Even if you have already created the datasheet, please look at the text of this paragraph. It can tell you how to operate Oracle with PHP+SQL technology.

In this case, we created a data table for storing personal emails.

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 handle Oracle databases, we first have to establish a connection with Oracle.
The syntax is ora_logon (user, password), which returns a Connectid.
Reminder: Before this we must also set the environment variable: ORACLE_SID value.

Now we can interact with Oracle through the ID of the join. The name of the data sheet is called Email_info. The table consists of 2 domains, one storage personal name, (such as: both) a storage email address such as (xiaoyue@163.net)

A cursor Ora_open is also required. This cursor is often used to enumerate data.  We query Oracle's result set with Ora_parse or ora_exec.  Ora_parse the correctness of the SQL syntax and ora_exec executes the corresponding SQL statement. If all this works, then we'll run ora_commit to confirm it.
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.