Home >Backend Development >PHP Tutorial >How to insert and update data from Oracle database in PHP

How to insert and update data from Oracle database in PHP

WBOY
WBOYOriginal
2023-07-12 12:52:451633browse

How to insert and update Oracle database data in PHP

In PHP development, interaction with the database is one of the very common operations. The Oracle database is a powerful and widely used relational database. This article will introduce how to insert and update data in Oracle database in PHP and provide code examples.

1. Configure the environment

Before you start, please make sure that PHP and Oracle database have been installed in your environment, and the relevant extension modules have been correctly configured. You can refer to Oracle official documentation for configuration.

2. Connect to the database

First, we need to connect to the Oracle database through PHP code. The following is a simple connection example:

<?php
// 数据库连接信息
$host = 'localhost';
$port = '1521';
$sid  = 'ORCL';
$username = 'your_username';
$password = 'your_password';

// 连接数据库
$conn = oci_connect($username, $password, "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$host.")(PORT=".$port."))(CONNECT_DATA=(SID=".$sid.")))");
if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
?>

Please modify the corresponding parameters according to your own environment configuration to ensure a successful connection.

3. Insert data

Next, we will demonstrate how to insert data in the Oracle database. The following is a simple insertion example:

<?php
// 插入数据
$query = "INSERT INTO your_table (column1, column2, column3) VALUES (:param1, :param2, :param3)";

$stmt = oci_parse($conn, $query);

// 绑定参数
oci_bind_by_name($stmt, ':param1', $value1);
oci_bind_by_name($stmt, ':param2', $value2);
oci_bind_by_name($stmt, ':param3', $value3);

// 执行插入
$result = oci_execute($stmt);
if ($result) {
    echo "插入成功";
} else {
    echo "插入失败";
}
?>

Please modify the corresponding table name and column name according to your own needs and data table structure, and bind the correct parameter values ​​for the insertion operation.

4. Update data

In addition to inserting data, we can also use PHP to update data in the Oracle database. The following is a simple update example:

<?php
// 更新数据
$query = "UPDATE your_table SET column1 = :param1, column2 = :param2 WHERE column3 = :param3";

$stmt = oci_parse($conn, $query);

// 绑定参数
oci_bind_by_name($stmt, ':param1', $value1);
oci_bind_by_name($stmt, ':param2', $value2);
oci_bind_by_name($stmt, ':param3', $value3);

// 执行更新
$result = oci_execute($stmt);
if ($result) {
    echo "更新成功";
} else {
    echo "更新失败";
}
?>

Please modify the corresponding table name and column name according to your own needs and data table structure, and bind the correct parameter values ​​for the update operation.

5. Disconnect

Finally, after completing the database operation, remember to disconnect from the Oracle database to release resources. The following is a simple example of closing the connection:

<?php
// 关闭连接
oci_close($conn);
?>

Through the above steps, you can insert and update data in the Oracle database in PHP. Of course, in actual development, there may be more complex scenarios and requirements, which need to be adjusted and expanded according to specific circumstances.

I hope this article can help you achieve better results in the development of PHP and Oracle database. Happy coding!

The above is the detailed content of How to insert and update data from Oracle database in 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