As a mature and stable database, Oracle is widely used in enterprise-level application development. As a commonly used server-side programming language, PHP can also be integrated with Oracle database. This article will introduce how to use Oracle database in PHP programming.
In Linux systems, you can use the following command to install the OCI extension:
pecl install oci8
In Windows systems, you can enable OCI extensions by modifying the php.ini configuration file.
Configure PHP running environment
After installing the OCI extension, you need to enable the OCI extension in the PHP configuration file php.ini. Find the following lines in php.ini and make sure they are enabled:
extension=oci8.so
or:
extension=php_oci8.dll
Additionally, the connection parameters for the Oracle database need to be set in php.ini:
oci8.connection_class = MYAPP oci8.default_prefetch = 100 oci8.events = Off oci8.max_persistent = -1 oci8.old_oci_close_semantics = Off oci8.persistent_timeout = -1 oci8.ping_interval = 60 oci8.privileged_connect = Off oci8.statement_cache_size = 20
When setting parameters, you need to adjust them according to the actual situation.
Connecting to the Oracle database
Using the Oracle database in PHP requires connecting through the oci_connect() function provided by the OCI extension. The parameters of the function include Oracle username, password and connection string. The name or service name, host name, and port number of the Oracle database need to be specified in the connection string. The sample code is as follows:
$connection = oci_connect('user', 'password', '//localhost/orcl');
Execute SQL statement
The SQL statement can be parsed into an executable cursor (cursor) through the oci_parse() function. For example, the following code can query an Oracle table containing numeric values and strings:
$statement = oci_parse($connection, "SELECT * FROM my_table"); oci_execute($statement);
If the SQL statement is executed incorrectly, the error information can be obtained through the oci_error() function provided by the OCI extension. For update operations, you can use the oci_commit() and oci_rollback() functions to commit or rollback the transaction.
Get the query results
You can get a row of records in the query result set through the following code:
$row = oci_fetch_assoc($statement);
oci_fetch_assoc() function returns an array in which the key is the result The name of each column in the set, and the corresponding value is the value of the corresponding column in the row record. It should be noted that if you want to fetch multiple rows of records, you need to add a loop within the oci_fetch_assoc() function.
The above is the basic process and method of using Oracle database in PHP programming. It should be noted that various errors are prone to occur during the integration process with the Oracle database. For common errors, you can obtain synchronous error information through the oci_error() function provided by the OCI extension, and use Oracle's own log function to analyze asynchronous database problems.
When using Oracle database, SQL statements need to be written rigorously. Especially for data insertion and update operations, relevant security checks and verifications need to be performed to avoid security issues such as injection attacks.
The above is the detailed content of How to use Oracle database in PHP programming?. For more information, please follow other related articles on the PHP Chinese website!