Windwos sharing _php instances using PHP to connect to Oracle databases

Source: Internet
Author: User
Tags install php parse error php code win32

To use PHP to connect to Oracle, the basic condition is
1. You need to install PHP,
2. Installation of Oracle,
3. Tnsname.ora is configured.
The local command line uses Sqlplus to connect to Oracle.

Select the 64bit or 32bit PHP program based on your version of the machine, and we use PHP's OCI8 extension to connect Oracle

After installing PHP, open the oci8 extension,

Write a section of ora.php code to connect to Oracle

Copy Code code as follows:

<?php

$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 "<table border= ' 1 ' >\n";
while ($row = Oci_fetch_array ($stid, oci_assoc+oci_return_nulls)) {
print "<tr>\n";
foreach ($row as $item) {
Print "<td>". ($item!== null htmlentities ($item, ent_quotes): ""). " </td>\n ";
}
print "</tr>\n";
}
print "</table>\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 Oracle's password
The third parameter is the connection string name in the Tnsnames.ora.

Execution under command line

Copy Code code as follows:
PHP ora.php

Prompt for the following error

Copy 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

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

Look at the 32-bit like 64-bit migration problem, we need to install Oracle Instant Client When the following problems arise.

Copy Code code as follows:

Unable to load dynamic library ' C:\Program Files (x86) \php\ext\php_oci8_11g.dll '-%1 was not a valid Win32.
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 and does not need to be installed.
If you have an Oracle account you can go to Oracle to download the corresponding version, (registered users need a bunch of information)

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

Troublesome students use this address to download

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

After downloading, unzip the compressed 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 not available, but will prompt

Copy Code code as follows:
Syntax error, unexpected t_constant_encapsed_string

The code is directly from the PHP official website, the code has invisible characters, use notepad++ to view all characters, remove garbled.

Continue execution, this prompt,

Copy 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 is relatively hurry, then I directly use the form of IP, the 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].
I specifically use the PHP OCI connection string is:
Copy Code code as follows:
$conn = Oci_connect (' hr ', ' welcome ', '//www.jb51.net:1523/sycx ');

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

PHP oci8 Chinese garbled solution, first query your Oracle database coding use,

Copy Code code as follows:
Select Userenv (' language ') from dual;

The result found is simplified Chinese_china. ZHS16GBK, set the environment variable in PHP code
Copy Code code as follows:
Putenv ("Nls_lang=simplified Chinese_china.") ZHS16GBK ");

Finally, PHP can 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.