How to use Oracle functions in PHP

王林
Release: 2023-05-18 21:04:01
Original
1601 people have browsed it

With the development of enterprise application software, many companies have begun to use Oracle databases to store data. In PHP development, Oracle database is widely used because it is a reliable and high-performance database. In order to better use Oracle database, you need to understand how to use Oracle functions in PHP. In this article, we will introduce how to use Oracle functions in PHP.

1. Connect to Oracle database

Before using Oracle functions, you must first connect to Oracle database. The Oracle database can be connected using the OCI8 library in PHP. First, you need to set the database parameters:

<?php
    //数据库用户名
    $user = "用户名";
    //数据库密码
    $password = "密码";
    //数据库主机名和端口号
    $db = "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=主机名)(PORT=端口号)))(CONNECT_DATA=(SID=服务名)))";
?>
Copy after login

Then, use the ocilogon function of the OCI8 library to connect to the database:

<?php
    //连接数据库
    $conn = ocilogon($user, $password, $db);
?>
Copy after login

2. Use the Oracle function

1. Query data

When using Oracle functions to query data, you can use the ociparse and ociexecute functions of the OCI8 library. Use the ociparse function to compile the SQL statement, and then use the ociexecute function to execute the SQL statement. For example:

<?php
    //查询SQL语句
    $sql = "SELECT name, age FROM person WHERE age > ?";
    
    //调用ociparse函数
    $stmt = ociparse($conn, $sql);
    
    //设置参数值
    $age = 18;
    ocibindbyname($stmt, ":p1", $age);
    
    //执行SQL语句
    ociexecute($stmt);
?>
Copy after login

2. Insert data

When using Oracle functions to insert data into the database, you can use the ociparse and ociexecute functions of the OCI8 library. Use the ociparse function to compile the SQL statement, and then use the ociexecute function to execute the SQL statement. For example:

<?php
    //插入SQL语句
    $sql = "INSERT INTO person(name, age) VALUES (?, ?)";
    
    //调用ociparse函数
    $stmt = ociparse($conn, $sql);
    
    //设置参数值
    $name = "张三";
    $age = 20;
    ocibindbyname($stmt, ":p1", $name);
    ocibindbyname($stmt, ":p2", $age);
    
    //执行SQL语句
    ociexecute($stmt);
?>
Copy after login

3. Update data

When using Oracle functions to update data in the database, you can use the ociparse and ociexecute functions of the OCI8 library. Use the ociparse function to compile the SQL statement, and then use the ociexecute function to execute the SQL statement. For example:

<?php
    //更新SQL语句
    $sql = "UPDATE person SET age = ? WHERE name = ?";
    
    //调用ociparse函数
    $stmt = ociparse($conn, $sql);
    
    //设置参数值
    $name = "张三";
    $age = 25;
    ocibindbyname($stmt, ":p1", $age);
    ocibindbyname($stmt, ":p2", $name);
    
    //执行SQL语句
    ociexecute($stmt);
?>
Copy after login

4. Delete data

When using Oracle functions to delete data from the database, you can use the ociparse and ociexecute functions of the OCI8 library. Use the ociparse function to compile the SQL statement, and then use the ociexecute function to execute the SQL statement. For example:

<?php
    //删除SQL语句
    $sql = "DELETE FROM person WHERE name = ?";
    
    //调用ociparse函数
    $stmt = ociparse($conn, $sql);
    
    //设置参数值
    $name = "张三";
    ocibindbyname($stmt, ":p1", $name);
    
    //执行SQL语句
    ociexecute($stmt);
?>
Copy after login

3. Close the connection

When you finish using the Oracle database, you need to close the connection. Use the ocilogoff function of the OCI8 library to close the connection. For example:

<?php
    //关闭连接
    ocilogoff($conn);
?>
Copy after login

Summary

This article introduces how to use Oracle functions in PHP development. It should be noted that before using Oracle functions, you must first use the OCI8 library to connect to the Oracle database. When using Oracle functions, you can use the ociparse and ociexecute functions of the OCI8 library to compile and execute SQL statements. Finally, the connection is closed using the ocilogoff function of the OCI8 library to release resources.

The above is the detailed content of How to use Oracle functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!