How to use php to connect to the oracle database in windwos

Source: Internet
Author: User
Tags parse error
This article mainly introduces how to use php to connect to the oracle database in windwos, explains the necessary conditions for connecting php to oracle, code instances, and troubleshooting, if you need to connect to oracle using php, refer to the following conditions:
1. you need to install php,
2. oracle,
3. configure tnsname. ora.
The local command line can use sqlplus to connect to oracle.

Select 64-bit or 32-bit php programs based on your machine version. We use the php oci8 extension to connect to oracle.

After installing php, open the oci8 extension,

Write a piece of oracle-connected ora. php code
The code is 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 );

?>

Note:
Oci_connect ('HR', 'Welcome ', 'mydb ')
The first parameter is the oracle user name,
The second parameter is the oracle password.
The third parameter is the connection string name in tnsnames. ora.

Run
The code is as follows: php ora. php

The following error is prompted:
The code is 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

At first, I thought I didn't select the correct version. I was a 64-bit machine and the result was a win32 program. as a literal prompt, I re-installed the new 32bit program or reported an error.

Check carefully and find the problem of 32-bit 64-bit migration. when the following problem occurs, we need to install the Oracle Instant Client.
The code is 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 that PATH already des the directory with Oracle Instant Client libraries

Oracle Instant Client, which is a program that can be used after decompression without installation.
If you have an oracle account, you can download the corresponding version from oracle. (a bunch of information is required for registered users)

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

Troublesome users use this address to download

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

Decompress the package to c: \ oracleinstantclient and add the PATH to the environment variable PATH.

Re-execute php ora. php. the error "% 1 is not a valid Win32 application" does not exist, but a prompt is displayed.
The code is as follows: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING
The code is copied directly from the php official website. The Code contains invisible characters. Use notepad ++ to view all the characters and remove the garbled characters.

Continue execution. this prompt,
The code is as follows:
PHP Fatal error: ORA-12154: TNS: cocould not resolve the connect identifier specified in C: \ Users \ nginx \ Desktop \ airline \ oci. php on line 6
It seems that php does not find the location of tnsnames. ora. if the time is too short, I will directly use the ip address format. the specific format is based on your information to spell the third parameter of oci_connect.
Oracle10 format: [//] host_name [: port] [/service_name]
Oracle11 format: [//] host_name [: port] [/service_name] [: server_type] [/instance_name].
The php oci connection string I used is:
The code is as follows: $ conn = oci_connect ('HR', 'Welcome ',' // www.bitsCN.com: 1523/sycx ');

After the above information is configured, the result is finally displayed. However, garbled characters are found in the result. this problem is basically caused by encoding mismatch.

Php oci8 Chinese garbled solution, first query your oracle database encoding usage,
The code is as follows: select userenv ('language') from dual;
The result is SIMPLIFIED CHINESE_CHINA.ZHS16GBK. set the environment variable in the php code.
The code is as follows: putenv ("NLS_LANG = SIMPLIFIED CHINESE_CHINA.ZHS16GBK ");

Finally, php can connect to oracle correctly.

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.