Home > Article > Backend Development > Sharing the process of using PHP to connect to Oracle database under Windows_PHP tutorial
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
$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 "
" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . " | 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
The following error is prompted
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.
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
Continue execution, this time prompt,
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,
Finally php can connect to oracle correctly.