Home > Article > Backend Development > How to connect to Oracle database using PDO
How to use PDO to connect to Oracle database
Overview:
PDO (PHP Data Objects) is an extension library for operating databases in PHP. It provides a unified API to access multiple types of database. In this article, we will discuss how to use PDO to connect to an Oracle database and perform some common database operations.
Steps:
;extension=php_pdo_oci.dll
;extension=php_oci8.dll
Create database connection
In the PHP program, we can use the following code to create a PDO object to connect to the Oracle database:
$dsn = 'oci:dbname=//hostname:port/oracle_sid'; $username = 'your_username'; $password = 'your_password'; try { $conn = new PDO($dsn, $username, $password); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); exit; }
In the code, $dsn Is a string containing the host name, port and Oracle SID. $username and $password are the username and password required to connect to the Oracle database.
Execute SQL Query
Once we successfully connect to the Oracle database, we can use the PDO object to execute the SQL query. The following is a simple example that demonstrates how to query data in the database and output the results to the browser:
$sql = 'SELECT * FROM employees'; $stmt = $conn->query($sql); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($result as $row) { echo $row['employee_id'] . ' ' . $row['first_name'] . ' ' . $row['last_name'] . '<br>'; }
In the code, $sql is the SELECT statement we want to execute, and $stmt is a PDOStatement object , which represents the result set of the query. By calling the fetchAll method of the PDOStatement object, you can obtain an associative array containing the query results. We can then use a foreach loop to iterate through the results and output the data for each row to the browser.
Execute prepared statements
In addition to ordinary SQL queries, PDO also supports executing prepared statements. Prepared statements can not only improve the performance of database operations, but also prevent SQL injection attacks. The following is a sample code that uses prepared statements to query the database:
$sql = 'SELECT * FROM employees WHERE department_id = :dept_id'; $stmt = $conn->prepare($sql); $stmt->bindParam(':dept_id', $dept_id); $dept_id = 1; $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($result as $row) { echo $row['employee_id'] . ' ' . $row['first_name'] . ' ' . $row['last_name'] . '<br>'; }
In the code, we use placeholders (:dept_id) to represent query conditions, and then use the bindParam method to bind the placeholders to the actual value. Next, we can execute the query by executing the execute method and save the results into the $result variable.
Summary:
Through the above steps, we can use PDO to successfully connect to the Oracle database and perform some common database operations. When developing PHP applications, using PDO to connect to the database not only improves performance, but also provides better security. I hope this article will help you understand how to use PDO to connect to an Oracle database.
The above is the detailed content of How to connect to Oracle database using PDO. For more information, please follow other related articles on the PHP Chinese website!