Home > Article > Backend Development > PHP realizes the connection and use of Oracle database
As a major development language, PHP can not only connect to MySQL database, but also to Oracle database. Oracle database is one of the largest commercial databases in the world and is widely used. This article will introduce how to use PHP to connect to the Oracle database, as well as some common operations.
1. Install the Oracle client
Before PHP can connect to the Oracle database, you need to install the Oracle client. The Oracle client is a standalone software package used to connect to the Oracle database and perform various operations. Before installing the Oracle client, please ensure that your system meets the following conditions:
Before installing the Oracle client, you need to know whether your system architecture is 32-bit or 64-bit. Download the appropriate Oracle client based on your system architecture. You can download the Oracle client from the Oracle official website. Download link: https://www.oracle.com/database/technologies/instant-client/downloads.html
After the download is complete, unzip the downloaded Oracle client to your system. Then add the path of the Oracle client to the system PATH environment variable.
2. Install PHP extension
Before connecting to the Oracle database, you need to install the PHP extension into your system. You can download this extension from the PECL website. Alternatively, use the following command from the command line to install the extension.
pecl install oci8
After installing this extension, you need to enable it in the PHP configuration file php.ini. In the php.ini file, add the following two lines to the end:
extension=oci8.so extension=pdo_oci.so
After saving the file, restart the PHP server.
3. Connect to Oracle database
After installing the Oracle client and PHP extension, you can connect to the Oracle database. In PHP, there are two ways to connect to an Oracle database: using oci8 or the PDO_OCI extension. Here we introduce using the PDO_OCI extension to connect to the Oracle database.
First, you need to create a PDO object. When creating a PDO object, you need to pass the following parameters: database host name, port number, database name, username and password.
$host = 'localhost'; $port = '1521'; $database = 'ORCL'; $username = 'username'; $password = 'password'; $dsn = "oci:dbname=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=$host)(PORT=$port)))(CONNECT_DATA=(SID=$database)))"; try { $pdo = new PDO($dsn, $username, $password); } catch (PDOException $e) { die("Error: " . $e->getMessage()); }
The above code will create a PDO object and connect to the Oracle database.
4. Execute SQL statements
Once connected to the Oracle database, you can start executing SQL statements. In PDO, there are two methods to execute SQL statements: prepare() and query().
prepare() method is used to prepare SQL statements, but it will not be executed immediately. After preparing the SQL statement, you can execute it using the execute() method.
query() method directly executes SQL statements.
The following is a sample code for using PDO to execute an INSERT statement:
$stmt = $pdo->prepare("INSERT INTO users (username, password) VALUES (:username, :password)"); $stmt->bindParam(':username', $username); $stmt->bindParam(':password', $password); $username = 'admin'; $password = 'password'; $stmt->execute();
Here, we use the prepare() method to prepare an INSERT statement, and then use the bindParam() method to bind the parameters. Finally, use the execute() method to insert the data into the database.
The sample code for using the query() method to execute a SELECT statement is as follows:
$result = $pdo->query("SELECT * FROM users"); foreach ($result as $row) { echo $row['username'] . "
";
}
Here, we use the query() method to execute the SELECT statement and use foreach Loop through the result set.
5. Summary
This article introduces how to use PHP to connect to the Oracle database and execute SQL statements. When using PDO to connect to the Oracle database, you need to install the Oracle client and PDO_OCI Extension. Once connected to the database, you can start executing SQL statements.
The above is the detailed content of PHP realizes the connection and use of Oracle database. For more information, please follow other related articles on the PHP Chinese website!