To use php to connect to oracle, the basic conditions are:
1. You need to install php,
2. Install oracle,
3. Configure tnsname.ora.
The local command line can connect to oracle using sqlplus.
Choose the 64bit or 32bit php program according to the version of your machine. We use php’s oci8 extension to connect to oracle
After installing php, open the oci8 extension,
Write a piece of ora.php code to connect to Oracle
Copy the 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 " " . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . " | n";
}
print "
n";
}
print "
n";
oci_free_statement($stid);
oci_close($conn);
?>
Instructions:
oci_connect('hr', 'welcome', 'MYDB')
The first parameter is the oracle username,
The second parameter is the oracle password
The three parameters are the connection string names in tnsnames.ora
Execute from the command line
Copy the code The code is as follows:
php ora.php
The following error is prompted
Copy the code The code is as follows:
PHP Warning: PHP Startup: Unable to load dynamic library ' C:phpphp_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:UsersnginxDesktoporaclephpocioci.php on line 3
At first I thought it was because I didn’t select the right version. I have a 64-bit machine, but it turned out that it was a win32 program. When I saw the text prompt, I reinstalled the new 32-bit program and still got an error.
After careful inspection, we found that the current 32-bit to 64-bit migration problems occur. When the following problems occur, we need to install Oracle Instant Client.
Copy code The code is as follows:
Unable to load dynamic library 'C:Program Files (x86)PHPextphp_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 includes the directory with Oracle Instant Client libraries
Oracle Instant Client, it is a program that can be used after decompression and does not require installation.
If you have an oracle account, you can go to oracle to download the corresponding version (registering a user requires a lot of information)
http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
Students who find it troublesome 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-execute php ora.php, the error "%1 is not a valid Win32 application" is gone, but the prompt will be
Copy the code The code is as follows :
syntax error, unexpected T_CONSTANT_ENCAPSED_STRING
The code is copied directly from the PHP official website. There are invisible characters in the code. Use notepad++ to view all characters and remove the garbled characters.
Continue execution, this time prompt,
Copy the code The code is as follows:
PHP Fatal error: ORA-12154: TNS:could not resolve the connect identifier specified in C:UsersnginxDesktopairlineoci.php on line 6
It seems that php cannot find the location of tnsnames.ora, and I am pressed for time, so I will use the ip format directly. The specific format is based on your information and 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 specific php oci connection string I use is :
Copy code The code is as follows:
$conn = oci_connect('hr', 'welcome', '//www.jb51. net:1523/sycx');
After configuring the above information, I finally got the results, but I found that the results contained garbled characters. This kind of problem is basically a mismatch in encoding.
To solve the Chinese garbled code in php oci8, first check the database encoding of your Oracle,
Copy the code The code is as follows:
select usenv('language') from dual;
The result found is SIMPLIFIED CHINESE_CHINA.ZHS16GBK, set the environment variable in the php code
Copy code The code is as follows:
putenv("NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK");
Finally php can connect to oracle correctly.
http://www.bkjia.com/PHPjc/776762.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/776762.htmlTechArticleTo use php to connect to oracle, the basic conditions are 1. You need to install php, 2. Install oracle, 3 .Configured tnsname.ora. The local command line uses sqlplus to connect to oracle. According to your machine...