Home Backend Development PHP Tutorial How to query data in Oracle database with PHP

How to query data in Oracle database with PHP

Jul 13, 2023 pm 07:34 PM
oracle Inquire php query oracle data - php

How to query data in Oracle database with PHP

With the advent of the Internet era, the development of websites and applications is becoming more and more common. As a key technology for data storage and management, database has also become one of the necessary tools for developers. Among them, Oracle database, as a powerful, stable and reliable relational database management system, has been widely used in enterprise-level applications. When developing a website or application, how to use PHP to query the Oracle database is a very important issue.

Before we begin, we need to ensure that the appropriate software environment is installed locally. The Oracle client software needs to be installed first, and then the Oracle extension for PHP (OCI8) can communicate with the Oracle database. After ensuring that the corresponding software is installed, we can start the following operations.

Step 1: Connect to Oracle database

First, we need to establish a connection with the Oracle database through PHP code. Use PHP's oci_connect() function to connect to the database. The specific code is as follows:

<?php
    $host = "localhost";  //Oracle主机地址
    $port = "1521";  //Oracle监听端口
    $sid = "ORCL";  //Oracle服务名或SID
    $username = "your_username";  //Oracle数据库用户名
    $password = "your_password";  //Oracle数据库密码
    
    // 使用oci_connect函数连接Oracle数据库
    $connect = oci_connect($username, $password, "$host:$port/$sid");
    
    // 判断是否连接成功
    if (!$connect) {
        $e = oci_error();
        trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
    }
    else {
        echo "连接Oracle数据库成功!";
    }
?>
Copy after login

Step 2: Query Oracle database data

After the connection is successful, we can query the Oracle database through PHP code Data. Use the oci_parse() function to parse the SQL query statement, and then use the oci_execute() function to execute the SQL statement. The specific code is as follows:

<?php
    // 连接Oracle数据库的代码省略
    
    // SQL查询语句
    $sql = "SELECT * FROM table_name";
    
    // 解析SQL查询语句
    $statement = oci_parse($connect, $sql);
    
    // 执行SQL查询语句
    $result = oci_execute($statement);
    
    // 判断是否查询成功
    if (!$result) {
        $e = oci_error($statement);
        trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
    }
    else {
        // 循环打印查询结果
        while ($row = oci_fetch_array($statement, OCI_ASSOC+OCI_RETURN_NULLS)) {
            foreach ($row as $item) {
                echo $item." ";
            }
            echo "<br>";
        }
    }
?>
Copy after login

In the above code, we use SELECT * FROM table_name to query all the data in the table, and use the oci_fetch_array() function to obtain the data of each row from the query results. Then use a loop to traverse the $row array and print out the data of each row. It should be noted that the two parameters OCI_ASSOC OCI_RETURN_NULLS are used to set the format of the data returned by the oci_fetch_array() function.

Step 3: Close the database connection

After completing the database query, we need to manually close the connection with the Oracle database to avoid occupying too many system resources. Use the oci_close() function to close the connection. The specific code is as follows:

<?php
    // 查询Oracle数据库的代码省略
    
    // 关闭与Oracle数据库的连接
    oci_close($connect);
    
    echo "关闭Oracle数据库连接成功!";
?>
Copy after login

At this point, we have completed the entire process of using PHP to query data in the Oracle database. Through the above steps, we can easily connect to the Oracle database and query the data in it. Of course, according to actual needs, we can also use the WHERE clause to add query conditions, and the ORDER BY clause to sort the query results, etc.

To summarize, using PHP to query data in an Oracle database is not complicated. The key is to first ensure that the Oracle client software is installed correctly and the PHP Oracle extension is configured. Then connect to the database through the oci_connect() function, use the oci_parse() function to parse the SQL query statement, use the oci_execute() function to execute the query statement, use the oci_fetch_array() function to obtain the query results, and print out the data of each row through a loop. Finally, use the oci_close() function to close the database connection and release system resources.

Through the above code examples, I believe readers have understood how to use PHP to query data in the Oracle database. It is hoped that readers can flexibly use this knowledge to develop more powerful and efficient websites and applications based on their actual situations.

The above is the detailed content of How to query data in Oracle database 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 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)

How to check tablespace size of oracle How to check tablespace size of oracle Apr 11, 2025 pm 08:15 PM

To query the Oracle tablespace size, follow the following steps: Determine the tablespace name by running the query: SELECT tablespace_name FROM dba_tablespaces; Query the tablespace size by running the query: SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

How to view instance name of oracle How to view instance name of oracle Apr 11, 2025 pm 08:18 PM

There are three ways to view instance names in Oracle: use the "sqlplus" and "select instance_name from v$instance;" commands on the command line. Use the "show instance_name;" command in SQL*Plus. Check environment variables (ORACLE_SID on Linux) through the operating system's Task Manager, Oracle Enterprise Manager, or through the operating system.

How to encrypt oracle view How to encrypt oracle view Apr 11, 2025 pm 08:30 PM

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

How to uninstall Oracle installation failed How to uninstall Oracle installation failed Apr 11, 2025 pm 08:24 PM

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

How to delete all data from oracle How to delete all data from oracle Apr 11, 2025 pm 08:36 PM

Deleting all data in Oracle requires the following steps: 1. Establish a connection; 2. Disable foreign key constraints; 3. Delete table data; 4. Submit transactions; 5. Enable foreign key constraints (optional). Be sure to back up the database before execution to prevent data loss.

How to check invalid numbers of oracle How to check invalid numbers of oracle Apr 11, 2025 pm 08:27 PM

Oracle Invalid numeric errors may be caused by data type mismatch, numeric overflow, data conversion errors, or data corruption. Troubleshooting steps include checking data types, detecting digital overflows, checking data conversions, checking data corruption, and exploring other possible solutions such as configuring the NLS_NUMERIC_CHARACTERS parameter and enabling data verification logging.

How to set up users of oracle How to set up users of oracle Apr 11, 2025 pm 08:21 PM

To create a user in Oracle, follow these steps: Create a new user using the CREATE USER statement. Grant the necessary permissions using the GRANT statement. Optional: Use the RESOURCE statement to set the quota. Configure other options such as default roles and temporary tablespaces.

What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

See all articles