Home  >  Article  >  Backend Development  >  How to connect to oracle remotely with php

How to connect to oracle remotely with php

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-08-21 17:30:423874browse

How to connect to oracle remotely with php

1. The following are the steps for accessing the oracle database using php based on wampServer:

The first step: let PHP supports OCI

First, install the integrated operating environment of PHP. There are many integrations on the Internet. I installed WampServer (for the specific installation method, you can also refer to another article I wrote before). Install After that, find the php.ini file from the installation directory. For example, my local path is D:\wamp\bin\php\php5.3.3. Remove the ";" of php_oci8.dll in php.ini, that is, remove the comments. , which is equivalent to using php_oci8.

How to connect to oracle remotely with php

Related recommendations: "php tutorial"

Step 2: Then after wampserver is running, change php>php Check php_oci8 in extentions

[Other integrated environments are also possible, such as phpStudy, we can directly check the corresponding ones from the php extension options].

How to connect to oracle remotely with php

Step 3: Oracle database file configuration

For PCs with Oracle client installed, you can install the configuration file, tnsnames.ora file, in Oracle. This file path is the path where Oracle is installed. For example, my local one is

F:\oracle\product\10.2.0\client_1\NETWORK\ADMIN, and the connected 192.168.1.198 database, where The configuration details are as follows (if 127.0.0.1 is displayed, it defaults to this machine):

(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST =  192.168.1.198)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)

Step 4: Check whether oci8 is configured successfully

1. Under normal circumstances, opening localhost will display phpinfo .php interface, which contains php information. You can use "Ctrl F" to search for "oci" to see if there is a corresponding oci module. Of course, if you have some PHP basics, you can directly access the files you wrote. Just remember to add "echo phpinfo();" in it.

2. Don’t be too happy. At this point, at least I can’t find the corresponding information. At this time, you can follow some suggestions on the Internet and put php_oci8 in the ext directory of php. Copy the dll to the system32 directory

3. Finally, it is recommended to restart the service, preferably the computer (I found during testing that restarting the service is useless. Once, the oci extension was accidentally refreshed. So if the operation method is correct, I recommend restarting.)

2. Code test to remotely connect to the orcal database (it is recommended to use your own oracle client to try whether you can connect to the other party's server to ensure Operation success rate)

 0) {
echo "\n";
 echo "\n";
 foreach ($results as $key => $val) {
echo "\n";
 }
echo "\n";
 for ($i = 0; $i < $nrows; $i++) {
echo "\n";
 foreach ($results as $data) {
echo "\n";
 }
echo "\n";
 }
echo "
$key
$data[$i]
\n"; } else { echo "No data found
\n"; } echo " $nrows Records Selected
\n"; oci_free_statement($stmt); oci_close($conn); ?>

(Refer to some instructions from netizens)

Two ways to establish a link with the oracle database:

1.$conn = oci_connect('username','password',"(DEscriptION=(ADDRESS=(PROTOCOL =TCP)(HOST=192.168.1.198)(PORT = 1521))
(CONNECT_DATA =(SID=orcl)))");
2.$conn = oci_connect('username','password','192.168.1.198/orcl');

Sometimes the first method doesn't work, so use the second one. The parameters are user name, password, and oracle service address, where orcl is the service name (but on my machine, the latter cannot be accessed)

In addition, a simple test code is provided. Relatively speaking, it only tests the connection situation, which is more convenient:




    
    php语句结束符


';
    if(OCILogOff($dbconn)==true)
    {
        echo "关闭连接成功!".'
';// } } else { echo "连接失败".'
'; } ?>

Summary tips:

make you PHP supports Oracle, just follow the following steps:

1. To install the PHP environment, look for appserv or xampp, and install it with one click, which is very convenient.

2. Copy php_oci8.dll in the ext directory of php to the system32 directory.

3. Modify the configuration in the php.ini file, remove ;extension = php_oci8.dll, and remove the preceding semicolon.

4. Restart apache.

Note:

1. Sometimes a mistake you won’t notice will waste a lot of time. I would also like to remind you, please remember Turn on oracle's service monitoring! !

2. Please remember to turn off the firewall on the PC as the server! !

3. The configuration file of apache is equally important. Modify the httpd.conf file, Deny——>Allow


     Options FollowSymLinks
     AllowOverride None
     Order deny,allow
 #    Deny from all
     Allow from all
 #允许所有访问
     Satisfy all


...
...
 #   Require local
 Options Indexes FollowSymLinks
 #   onlineoffline tag - don't remove
     Order Deny,Allow
     Allow from all
 #   Require local

The above is the detailed content of How to connect to oracle remotely with php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn