Share _php instances using PHP to connect to Oracle database under WINDWOS

Source: Internet
Author: User
Tags parse error php website
To connect to Oracle using PHP, the basic condition is
1. You need to install PHP,
2. Installation of Oracle,
3. The Tnsname.ora is configured.
The local command line uses Sqlplus to connect to Oracle.

Depending on the version of your machine, choose PHP for 64bit or 32bit, we use PHP's OCI8 extension to connect Oracle

After installing PHP, open the oci8 extension,

Write a ora.php code that connects to Oracle
Copy the Code code as follows:

$conn = Oci_connect (' hr ', ' welcome ', ' MYDB ');
if (! $conn) {
$e = Oci_error ();
Trigger_error (Htmlentities ($e [' message '], ent_quotes), e_user_error);
}

Prepare the statement
$stid = Oci_parse ($conn, ' SELECT * from departments ');
if (! $stid) {
$e = Oci_error ($conn);
Trigger_error (Htmlentities ($e [' message '], ent_quotes), e_user_error);
}

Perform the logic of the query
$r = Oci_execute ($stid);
if (! $r) {
$e = Oci_error ($stid);
Trigger_error (Htmlentities ($e [' message '], ent_quotes), e_user_error);
}

Fetch the results of the query
Print "









\ n "; while ($row = Oci_fetch_array ($stid, oci_assoc+oci_return_nulls)) {print" \ n "; foreach ($row as $item) {print ' \ n "; } print " \ n ";} Print "
" . ($item!== null? htmlentities ($item, ent_quotes): ""). "
\ n ";

Oci_free_statement ($stid);
Oci_close ($conn);

?>

Description
Oci_connect (' hr ', ' welcome ', ' MYDB ')
The first parameter is the user name of Oracle,
The second parameter is the Oracle password
The third parameter is the connection string name in the Tnsnames.ora

Execute under command line
Copy the Code code as follows: PHP ora.php

Prompt for the following error
Copy the Code code as follows:
PHP warning:php startup:unable to load Dynamic Library ' C:\php\php_oci8.dll '-%1 is not a valid WIN32 application. In Unknown on line 0
PHP Parse error:syntax error, unexpected ' "User" ' (t_constant_encapsed_string) in C:\Users\nginx\Desktop\oraclephpoci\ Oci.php on line 3

began to think that there is no election to the version, I am a 64-bit machine, the result is said to be Win32 program, a look at the literal hint, I reinstall the new 32bit program or error.

Check it out. We need to install the Oracle Instant Client when the following problem occurs in 32-bit like 64-bit migration.
Copy the Code code as follows:
Unable to load dynamic library ' C:\Program Files (x86) \php\ext\php_oci8_11g.dll ' –%1 is not a valid WIN32 application.
Warning oci_connect (): Ocienvnlscreate () failed. There is something wrong with your system-please check this PATH includes the directory with Oracle Instant Client Libra Ries

Oracle Instant Client, which is a program that can be used after decompression, does not need to be installed.
If you have an Oracle account, you can go to Oracle to download the corresponding version, (the registered user needs a bunch of information)

Http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html

Troublesome classmates Use this address to download

http://eduunix.ccut.edu.cn/index2/database/Oracle%20Instant%20Client/

After download, unzip the package to C:\oracleinstantclient and add the path to the environment variable path

Re-executing PHP ora.php, "%1 is not a valid WIN32 application" error is not available, but will prompt
Copy the Code Code as follows: syntax error, unexpected t_constant_encapsed_string
Code is directly copied from the PHP website, the code has invisible characters, using notepad++ to view all characters, remove garbled.

Continue, this prompt,
Copy the Code code as follows:
PHP Fatal error:ora-12154:tns:could Not resolve the connect identifier specified in C:\USERS\NGINX\DESKTOP\AIRLINE\OCI.P HP on line 6
It seems that PHP did not find the location of Tnsnames.ora, time compared to rush, then I directly use the form of IP, specific format according to your information spelling Oci_connect the third parameter
ORACLE10 format: [//]host_name[:p Ort][/service_name]
ORACLE11 format: [//]host_name[:p Ort][/service_name][:server_type][/instance_name].
The specific PHP OCI connection string I use is:
Copy the code as follows: $conn = Oci_connect (' hr ', ' welcome ', '//www.php.net:1523/sycx ');

With the above information, and finally can produce results, but found out the results of the asked garbled, this problem is basically a coding mismatch.

PHP oci8 Chinese garbled solution, first query your Oracle database encoding use,
Copy the Code code as follows: Select Userenv (' language ') from dual;
The result is simplified Chinese_china. ZHS16GBK, set environment variables in PHP code
Copy the Code code as follows: Putenv ("Nls_lang=simplified Chinese_china. ZHS16GBK ");

Finally, PHP is able to connect to Oracle correctly.

  • 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.