How to use Oracle databases in PHP

Source: Internet
Author: User
Tags exit html form insert php code php development environment oracle database

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.

Create a Table Using OCI

Below we will create an email personal information book. This time using the OCI8 API Directive

related PHP code:

!--? php

PutEnv ("Oracle_sid=orasid");

$connection = Ocilogon ("username", "password");
if ($connection = = False) {
   echo ocierror ($connection).
";
   exit;
}   

$query = "CREATE Table Email_info".
              (fullname varchar (255), email_address varchar (255)) ";

$cursor = Ociparse ($connection, $query);
if ($cursor = = False) {
   echo ocierror ($cursor). "
";
   exit;
}

$result = Ociexecute ($cursor);
if ($result = = False) {
   echo ocierror ($cursor). "
";
   exit;
}

Ocicommit ($connection);
Ocilogoff ($connection);

?>

We can see that the 2 code syntax is almost the same, the difference is only the function name is different, and secondly, in OCI8 we do not need to run the instructions to open the cursor specifically in the call ociparse The system automatically returns a cursor ID.

using ORA to enter data into the datasheet ' Email_info '

When the user browses this script, displays a form that consists of a name, an email input field, and when the user adds a good data click Submit, the script will save the name and email to the ' email_info ' datasheet.

Related PHP Code:

-->
if ($submit = = "click") {
The Submit button was clicked!
Get the input for fullname and email then store it in the database.
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 = "INSERT into email_info values (' $fullname ', ' $email ')";
$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);
}
else{
Echo '


<form action=insert.php method=post>

Please enter your name
<input name=fullname> </INPUT>

Please enter your email address
<input name=email> </INPUT>

<input name=submit type=submit value=click> </INPUT>

</FORM>


';
}

?>

Yes, this script must be saved as insert.php because the insert.php is specified as the form handler in the calling page

Browse Effect:



Please enter your name
Please enter your email address


using OCI to enter data into the datasheet ' Email_info '

Ditto, just use OCI to write

Related PHP Code:

-->
if ($submit = = "click") {
The Submit button was clicked!
Get the input for fullname and email then store it in the database.
PUTENV ("Oracle_sid=orasid");

$connection = Ocilogon ("username", "password");
if ($connection = = False) {
echo Ocierror ($connection). "
";
Exit
}

$query = "INSERT into email_info values (' $fullname ', ' $email ')";
$cursor = Ociparse ($connection, $query);
if ($cursor = = False) {
echo Ocierror ($cursor). "
";
Exit
}

$result = Ociexecute ($cursor);
if ($result = = False) {
echo Ocierror ($cursor). "
";
Exit
}

Ocicommit ($connection);
Ocilogoff ($connection);
}
else{
Echo '


<form action=insert.php method=post>

Please enter your name
<input name=fullname> </INPUT>

Please enter your Email address
<input name=email> </INPUT>

<input name=submit type=submit value=click> </INPUT>

</FORM>


';
}

?>

Yes, this script must be saved as insert.php because the insert.php is specified as the form handler in the calling page

Browse Effect:



Please enter your name
Please enter your email address

Use Ora to list data in all datasheet ' Email_info '


Below, we will read out the contents of the database, and display the data in the ' Email_info ' datasheet as an HTML form

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 = "SELECT * from Email_info";
$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
}

echo "











"; Echo" while (Ora_fetch_into ($cursor, & $values)) {$name = $values [0];  $email = $values [1]; echo " ";} echo "
Full Name Email Address
$name $email
";

Ora_close ($cursor);
Ora_logoff ($connection);

?>

The browsing effect of the program running is as follows:

Name Email address
Spring Springflower@163.com
Autumn Moon Autumnmoon@163.com
... ...

Use OCI to list data in all datasheet ' Email_info '


Ditto, just use OCI to write

Related PHP Code:


PUTENV ("Oracle_sid=orasid");

$connection = Ocilogon ("username", "password");
if ($connection = = False) {
echo Ocierror ($connection). "
";
Exit
}

$query = "SELECT * from Email_info";
$cursor = Ociparse ($connection, $query);
if ($cursor = = False) {
echo Ocierror ($cursor). "
";
Exit
}

$result = Ociexecute ($cursor);
if ($result = = False) {
echo Ocierror ($cursor). "
";
Exit
}

echo "











"; Echo" while (Ocifetchinto ($cursor, $values)) {$name = $values [0];  $email = $values [1]; echo " ";} echo "
Full Name Email Address
$name $email
";

Ocilogoff ($connection);

?>

The browsing effect of the program running is as follows:

Name Email address
Spring Springflower@163.com
Autumn Moon Autumnmoon@163.com
... ...



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.