PHP connects Oracle database and queries data SQL

Source: Internet
Author: User
Tags oracle database

Remember that PHP supports Oracle connection functions

php.ini files in the configuration, remove; extention = Php_oci8.dll, remove the preceding semicolon
You can restart Apache,
If not, we'll copy the Php_oci8.dll in the PHP directory to the system32 under Windows System.
Oracle Database link creation

  code is as follows copy code

1. $conn = Oci_connect (' username ', ' password ', "(description= address= (PROTOCOL =tcp) (host=192.168.1.100) (PORT = 1521)) ( Connect_data = (sid=test)));

2. $conn = Oci_connect (' username ', ' password ', ' 192.168.1.100/test ');

3.Oracle Connection method:

Set Adocon=server.createobject ("Adodb.connection")
Adocon.open "Driver={microsoft ODBC for oracle};server= Oraclesever.world;uid=admin;pwd=pass; "

4.Oracle OLE DB connection method:

Set Adocon=server.createobject ("Adodb.connection")
Adocon.open "Provider=oraoledb.oracle;data source=dbname ; user id=admin;password=pass; "

Sometimes the first way is not, use the second, the inside of several parameters are user name, password, Oracle service address, where test is the service name.

The code is as follows Copy Code

$sql = "SELECT * FROM Table_exmaple"
$ora _test = Oci_parse ($conn, $sql); Compiling SQL statements
Oci_execute ($ora _test,oci_default); Perform
while ($r =oci_fetch_row ($ora _test))//Retrieve results
{
echo $ora _test[0];
echo "<BR>";
}

Look at a complete example.

If the PHP version >5.0, use the following function

Oci_connect (username, password, dbname)

Example:

The code is as follows Copy Code

<?php

$conn = Oci_connect (' hr ', ' hr ', ' ORCL '); Establish a connection

if (! $conn) {

$e = Oci_error ();

Print htmlentities ($e [' message ']);

Exit

}

$query = ' SELECT * from departments '; Query statement

$stid = Oci_parse ($conn, $query); Configuring SQL statements, preparing to execute

if (! $stid) {

$e = Oci_error ($conn);

Print htmlentities ($e [' message ']);

Exit

}

$r = Oci_execute ($stid, Oci_default); Execute SQL. Oci_default says don't commit automatically

if (! $r) {

$e = Oci_error ($stid);

Echo htmlentities ($e [' message ']);

Exit

}

Print execution Results

print ' <table border= ' 1 ' > ';

while ($row = Oci_fetch_array ($stid, oci_return_nulls)) {

print ' <tr> ';

foreach ($row as $item) {

print ' <td> '. ($item? Htmlentities ($item): '). ' </td> ';

}

print ' </tr> ';

}

print ' </table> ';

Oci_close ($conn);

?>

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.