As an open source high-level programming language, PHP's advantages in application development have been widely recognized. In enterprise-level application development, it is often necessary to interact with a relational database. Oracle database is a relational database that is currently widely used. This article will introduce in detail how to use PHP to connect to the Oracle database.
Before using PHP to connect to the Oracle database, you need to install the Oracle client first. Oracle provides client installation packages for different platforms. Download the corresponding version of the client installation package for installation. Here we take the Windows platform as an example, assuming that the installation path is C: oracleclient.
PHP provides OCI8 extension to support connection and interaction with Oracle database. After installing the Oracle client, you need to download the corresponding version of the OCI8 extension. The OCI8 extension can be downloaded from the PECL (PHP Extension Community Library) website or installed from source code. Here we take source code installation as an example.
First, you need to download the source code package and the PHP configuration file php.ini. Under the Windows platform, you can download the corresponding version of the PHP source code package from the official PHP website (https://windows.php.net/download/). Here, PHP 7.3 is used as an example. After downloading, unzip it to the C:php-7.3 directory.
Then, download the latest version of the OCI8 extension source code package from the PECL (https://pecl.php.net/package/oci8) website. After decompression, copy the oci8 directory to the C:php-7.3ext directory.
Next, switch to the C:php-7.3 directory on the command line and execute the following command:
php -r "copy('php.ini-development', 'php.ini');"
This command will copy the php.ini-development file to php.ini, php .ini is PHP's main configuration file, where extensions and other options can be configured.
Open the php.ini file and add the following content at the end:
extension_dir = "C:php-7.3ext" extension=oci8
Save and exit the php.ini file. Then, execute the following command on the command line:
cd C:php-7.3 php.exe -f configure.php --disable-all --enable-cli --with-oci8=shared,instantclient,C:oracleclient
This command will compile PHP and compile the OCI8 extension into it. Among them, the --with-oci8 parameter specifies the type and location of the OCI8 extension, the --disable-all parameter disables all extensions, and the --enable-cli parameter enables the command line interface.
After installing the Oracle client and OCI8 extension, you can establish a connection to the Oracle database through PHP code. The following is a PHP code example:
<?php $conn = oci_connect('username', 'password', 'hostname/servicename'); if (!$conn) { $e = oci_error(); trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR); } $sql = 'SELECT * FROM table_name'; $stmt = oci_parse($conn, $sql); oci_execute($stmt); while (($row = oci_fetch_array($stmt, OCI_ASSOC)) != false) { echo $row['COLUMN_NAME_1'] . ' ' . $row['COLUMN_NAME_2'] . " "; } oci_free_statement($stmt); oci_close($conn); ?>
The above code first uses the oci_connect function to establish a connection with the Oracle database. The three parameters are user name, password, and Oracle service name. If the connection fails, an error message will be output and it will exit.
Next, define a SQL statement and use the oci_parse function to prepare the query statement. Then, use the oci_execute function to execute the query statement, and use the oci_fetch_array function to obtain the query results. Finally, use the oci_free_statement function and oci_close function to release resources and close the connection.
This article details how to use PHP to connect to the Oracle database. It should be noted that in the process of using OCI8 extension, you may encounter some problems, which need to be debugged and solved according to the specific situation. At the same time, in the process of using Oracle database, you also need to master the basic operating skills of SQL language and Oracle database.
The above is the detailed content of Connect to Oracle database using PHP. For more information, please follow other related articles on the PHP Chinese website!