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

How to connect to oracle remotely with php

Aug 21, 2019 pm 05:30 PM
oracle php connect

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)
)
)
Copy after login

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);
?>
Copy after login

(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)))");
Copy after login
2.$conn = oci_connect(&#39;username&#39;,&#39;password&#39;,&#39;192.168.1.198/orcl&#39;);
Copy after login

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>
Copy after login

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>
Copy after login

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 of this Website
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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

See all articles