Home >Backend Development >PHP Tutorial >How to connect to oracle remotely with php

How to connect to oracle remotely with php

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

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)

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/12/7
 * Time: 16:25
 */
echo &#39;ff&#39;;
//进行连接数据库的参数配置
$dbstr ="(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST =192.168.11.198)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
(INSTANCE_NAME = orcl)))";
//phpinfo();
$conn = oci_connect(&#39;scott&#39;,&#39;tiger&#39;,$dbstr);//如果去掉最后一个参数或者为“ ”,默认连接本机
$stmt = oci_parse($conn, "select * from mono");
oci_execute($stmt);
$nrows = oci_fetch_all($stmt, $results);
if ($nrows > 0) {
echo "<table border=\"1\">\n";
 echo "<tr>\n";
 foreach ($results as $key => $val) {
echo "<th>$key</th>\n";
 }
echo "</tr>\n";
 for ($i = 0; $i < $nrows; $i++) {
echo "<tr>\n";
 foreach ($results as $data) {
echo "<td>$data[$i]</td>\n";
 }
echo "</tr>\n";
 }
echo "</table>\n";
} else {
echo "No data found<br />\n";
}
echo " $nrows  Records Selected<br />\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(&#39;username&#39;,&#39;password&#39;,"(DEscriptION=(ADDRESS=(PROTOCOL =TCP)(HOST=192.168.1.198)(PORT = 1521))
(CONNECT_DATA =(SID=orcl)))");
2.$conn = oci_connect(&#39;username&#39;,&#39;password&#39;,&#39;192.168.1.198/orcl&#39;);

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:

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>php语句结束符</title>
</head>
<body>
<?php
$dbstr ="(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST =192.168.11.102)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
(INSTANCE_NAME = orcl)))";
$dbconn=oci_connect(&#39;scott&#39;,&#39;tiger&#39;,$dbstr);
if($dbconn!=false)
{
    echo "连接成功".&#39;<br/>&#39;;
    if(OCILogOff($dbconn)==true)
    {
        echo "关闭连接成功!".&#39;<br/>&#39;;//
    }
}
else
{
    echo "连接失败".&#39;<br/>&#39;;
}
?>
</body>
</html>

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

<Directory />
     Options FollowSymLinks
     AllowOverride None
     Order deny,allow
 #    Deny from all
     Allow from all
 #允许所有访问
     Satisfy all
</Directory>
<Directory />
...
...
 #   Require local
 Options Indexes FollowSymLinks
 #   onlineoffline tag - don&#39;t remove
     Order Deny,Allow
     Allow from all
 #   Require local
</Directory>

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