search
HomeBackend DevelopmentPHP TutorialHow to connect to oracle remotely with php

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
How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

Increase PHP Performance: Caching Strategies & TechniquesIncrease PHP Performance: Caching Strategies & TechniquesMay 11, 2025 am 12:08 AM

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.