It seems strange. Why are PHP and oracle? Generally, they are not PHP and Mysql. They are the golden partners đ¤Ŗīŧ Everything comes from free. Oracle database is collected for free. As the saying goes, it's useless đ¤Šīŧ Since there is this resource, it is like how to use it, and then start the whole process, because the articles on the network are old and are no longer applicable to the new version of database and PHP version. As a result, we encountered many pits. After jumping pits for a week, we finally landed successfully. It is hereby recorded âī¸
1. Linux version
- Server: CentOS7
- PHP version: v7.0 four point three
- Oracle database version: 19c
1.1 install oracle client
First, go to Oracle's official website to download three packages to the local computer, and download the link:
https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html
- oracle-instantclient-devel-21.1.0.0.0-1.x86_64.rpm
- oracle-instantclient-basic-21.1.0.0.0-1.x86_64.rpm
- oracle-instantclient-sqlplus-21.1.0.0.0-1.x86_64.rpm
Upload these three packages to / usr/lib/oracle directory (oracle folder is a new folder), enter the folder and execute the installation command:
$ rpm -ivh oracle-instantclient-*
Add environment variables and save:
$ vim /etc/profile # Add the following at the end export ORACLE_HOME=/usr/lib/oracle/21/client64 export ORACLE_BASE=/usr/lib/oracle/21 export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH export PATH=$ORACLE_HOME/bin:$PATH
After saving and exiting, execute the following command to make the environment variable take effect:
$ source /etc/profile
đ Note: install oci8 and PDO_ This first step is required for all OCI extensions
1.2 installing oci8 extensions
Since compilation and installation often report various errors, pecl installation is recommended here:
$ export PHP_DTRACE=yes $ pecl install oci8-2.2.0 # enter path instantclient,/usr/lib/oracle/21/client64/lib
đ If an error is reported, cannot find sys / SDT h which is required for DTrace support
Execute installation: Yum install systemtap SDT devel
The following information indicates that the extension was installed successfully:
Build process completed successfully Installing '/usr/lib64/php/modules/oci8.so' install ok: channel://pecl.php.net/oci8-2.2.0 configuration option "php_ini" is not set to php.ini location You should add "extension=oci8.so" to php.ini
In PHP Add extension = oci8. Ini so :
$ vi /etc/php.ini # add to extension=oci8.so # Verify success $ php -m | grep oci8 # oci8 indicates successful installation oci8 # You need to modify / usr / lib64 / PHP / modules / oci8 So file permissions 755
1.3 installation of pdo_oci extension
vi /etc/ld.so.conf # Join this line, save and exit /usr/lib/oracle/21/client64/lib/
Establish a soft connection. If it has been established when installing oci8 extension, skip it
ln -s /usr/lib/oracle/21/client64 /usr/lib/oracle/21/client ln -s /usr/include/oracle/21/client64 /usr/include/oracle/21/client
Go to PHP official download php-7.4.19 tar. GZ compressed file, and PDO after decompression_ Put the OCI folder into the PHP extension directory of the server: cd /usr/include/php/ext, and then execute the following command:
# Execute the following command to compile and install, similar to oci8 installation $ cd /usr/include/php/ext/pdo_oci $ /usr/bin/phpize $ ./configure --with-pdo-oci=instantclient,/usr/lib/oracle/21/client64/lib \ --with-oci8=instantclient,/usr/lib/oracle/21/client64/lib $ make && make install
Add pdo_oci extension:
$ vi /etc/php.d/pdo_oci.ini # pdo_oci.ini is new $ extension=/usr/lib64/php/modules/pdo_oci.so # PDO here_ oci. The full path of so is displayed after compilation
Restart PHP service:
systemctl restart httpd systemctl restart php-fpm
1.4 test PHP connection to Oracle Database
First, prepare the connection file, download the Oracle wallet file from the Oracle database, download it from the Oracle database background management, and extract the following files:
Put the folder in a directory on the server( đ Keep it confidential. I put it here in / usr/lib/oracle/Wallet_videoplatform2, and then PHP test code is as follows:
<?php // tnsnames. Splicing database connection information in ora information (just follow the spelling) $dbstr ="tcps://adb.ap-seoul-1.oraclecloud.com:1522/ge815e794bb3bb9_xxxxxxxxx_high.adb.oraclecloud.com?wallet_location=/usr/lib/oracle/Wallet_videoplatform2"; $dbconn = oci_connect('user name','password',$dbstr); if($dbconn!=false) { echo "Connection successful".'<br/>'; if(OCILogOff($dbconn)==true) { echo "Connection closed successfully!".'<br/>';// } } else { echo "connection failed".'<br/>'; } echo phpinfo();
đ If ORA-12546: TNS:permission denied error occurs, execute the following statement:
setsebool -P httpd_can_network_connect on
Visit the page and the result:
Finally connected! âī¸
2. Windows version
2.1 installation of OCI8
PHP7 needs to select oci version 2.2.0. Select the version according to your own PHP version:
Download link: https://pecl.php.net/package/oci8/2.2.0/windows
You can check the PHP Extension Build field of phpinfo() first. My PHP is of NTS version, so the download oci should also be of NTS version.
After downloading, copy the extracted file to the PHP extension directory php7 4.3nts \ ext overwrite the original file, and the extended directory is in PHP Extension in ini_ Dir = specified by XXXXXX.
2.2 installing Oracle Client
Oracle Instant Client is a set of free libraries that can be easily installed, allowing programs to connect to local or remote Oracle database instances.
- First download the oracle client Oracle official client download What I download here is instantclient basic windows x64-19.11.0.0.0dbru. Zip version.
- Extract the package into a single directory, for example, D: \ Oracle \ instantclient_ 19_ eleven
- Add this directory to the PATH environment variable. If you have multiple versions of Oracle libraries installed, make sure that the new directory appears in the PATH first. Restart any terminal windows, otherwise make sure your application uses the new PATH.
After downloading, there is OCI dll, when the php extension is started, the oracle file is found. How can php find this dll? Just add the system environment variable.
Then modify PHP Ini configuration file:
# Remove the preceding semicolon; Open the extension. If not, add it extension=pdo_oci extension=php_oci8.dll
Finally, restart Apache.
To check whether the installation is successful, you can use the cmd command line to check whether the oci8 extension has been successfully installed. If the installation is successful, the following information will be displayed:
You can also view the extension information in phpinfo():
2.3 test PHP connection to Oracle Database
First, prepare the connection file, download the oracle wallet file from the oracle database, and extract the following files:
Put the files in a local directory, where I put them in D:\oracle\Wallet_videoplatform2, and then test the code:
<?php // tnsnames. Splicing database connection information in ora information (just follow the spelling) $dbstr ="tcps://adb.ap-seoul-1.oraclecloud.com:1522/xxxxx_videoplatform2_high.adb.oraclecloud.com?wallet_location=D:\oracle\Wallet_videoplatform2"; $conn = oci_connect('ADMIN','xxxxxxx',$dbstr); if (!$conn) { $e = oci_error(); trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR); } // Query and render $stid = oci_parse($conn, 'SELECT * FROM users'); $r = oci_execute($stid); print "<table border='1'>\n"; while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) { print "<tr>\n"; foreach ($row as $item) { print " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>\n"; } print "</tr>\n"; } print "</table>\n"; oci_free_statement($stid);
The database access success page displays:
3. Summary
Everyone may be the first person to eat crabs. If you have a chance, don't be afraid. Seize the opportunity and spell it, you may taste the sweetness.
It's not easy to climb the pit. With the support of praise đš
PS: there are children's shoes. I want to know how to get the permanent free server and permanent free database. I'll tell you secretly đ