php 8.1.x
os : centos 7.x
먼저 리눅스에 설치할 oracle-instantclent 를 다운받아 설치를 진행한다
다운로드 주소는 https://www.oracle.com/kr/database/technologies/instant-client/linux-x86-64-downloads.html
1 2 3 4 5 6 7 |
[root@localhost ~]# wget https://download.oracle.com/otn_software/linux/instantclient/1916000/oracle-instantclient19.16-basic-19.16.0.0.0-1.x86_64.rpm [root@localhost ~]# wget https://download.oracle.com/otn_software/linux/instantclient/1916000/oracle-instantclient19.16-devel-19.16.0.0.0-1.x86_64.rpm [root@localhost ~]# rpm -Uvh oracle-instantclient19.16-basic-19.16.0.0.0-1.x86_64.rpm [root@localhost ~]# rpm -Uvh oracle-instantclient19.16-devel-19.16.0.0.0-1.x86_64.rpm |
php 8.1. 버전에 맞는 oci 버전을 다운로드 (https://pecl.php.net/package/oci8 ) /
php 7.3 버전의 경우 맞는 oci 버전을 다운로드 ( oci8-2.2.0.tgz )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[root@localhost ~]# wget https://pecl.php.net/get/oci8-3.2.0.tgz [root@localhost ~]# tar xvfz oci8-3.2.0.tgz [root@localhost ~]# cd oci8-3.2.0 [root@localhost oci8-3.2.0]# /usr/local/php/bin/phpize Configuring for: PHP Api Version: 20210902 Zend Module Api No: 20210902 Zend Extension Api No: 420210902 [root@localhost oci8-3.2.0]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config --with-oci8=shared,instantclient,/usr/lib/oracle/19.16/client64/lib/ [root@localhost oci8-3.2.0]# make [root@localhost oci8-3.2.0]# make install Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/debug-non-zts-20210902/ |
php.ini 파일에 등록
1 2 3 4 |
[root@xinet oracle]# vi /usr/local/apache/conf/php.ini extension=oci8.so |
먼저 오라클 사용자로 접속해서 테이블 생성 및 데이터 입력
오라클 사용자 추가 및 권한 설정은 해당 페이지 참고 -> https://xinet.kr/?p=3173
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
[oracle@localhost ~]$ sqlplus SQL*Plus: Release 12.2.0.1.0 Production on Fri Sep 30 15:03:37 2022 Copyright (c) 1982, 2016, Oracle. All rights reserved. Enter user-name: jsh Enter password: Last Successful login time: Fri Sep 30 2022 14:26:53 +09:00 Connected to: Oracle Database 12c Standard Edition Release 12.2.0.1.0 - 64bit Production 테이블 생성 및 자료 입력 create table students ( student_no varchar(10), surname varchar(20), forename varchar(20)); create table modules ( module_code varchar(8), module_name varchar(20)); create table marks ( student_no varchar(10), module_code varchar(8), mark integer); insert into students values ('20220101','jin','sunghoon'); insert into students values ('20220102','kang','sangho'); insert into students values ('20220103','kim','jongmoon'); insert into students values ('20220104','nam','yoonki'); insert into students values ('20220105','hong','namchul'); insert into modules values ('CM0001', 'Databases'); insert into modules values ('CM0002', 'Programming Languages'); insert into modules values ('CM0003', 'Operating Systems'); insert into modules values ('CM0004', 'Graphics'); insert into marks values (''20220101', 'CM0001', 80); insert into marks values (''20220101', 'CM0002', 65); insert into marks values (''20220101', 'CM0003', 50); insert into marks values (''20220102', 'CM0001', 75); insert into marks values (''20220102', 'CM0003', 45); insert into marks values (''20220102', 'CM0004', 70); insert into marks values (''20220103', 'CM0001', 60); insert into marks values ('20220103', 'CM0002', 75); insert into marks values ('20220103', 'CM0004', 60); insert into marks values ('20220104', 'CM0001', 55); insert into marks values ('20220104', 'CM0002', 40); insert into marks values ('20220104', 'CM0003', 45); insert into marks values ('20220105', 'CM0001', 55); insert into marks values ('20220105', 'CM0002', 50); insert into marks values ('20220105', 'CM0004', 65); commit; |
php 샘플 파일을 만든다
여기서 jsh 사용자 아이디
qwer1234111#$ 사용자 패스워드
192.168.1.10 : 오라클 서버 아이피
orcl : 오라클 인스턴스 네임
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
[root@localhost html]# vi oracle_connect.php <title>Oracle in PHP test page</title> <h1>PHP Access an Oracle database</h1> <?php $conn = oci_connect('jsh','qwer1234111#$','192.168.1.10/orcl','UTF8'); // prepare the query $stmt = "SELECT s.student_no, surname, forename, module_code, mark FROM students s, marks m WHERE module_code = 'CM0001' AND m.student_no = s.student_no ORDER BY mark DESC"; $stid = oci_parse($conn, $stmt); // execute the query if( !oci_execute($stid) ) { $e = oci_error(); echo htmlentities($e['message'], ENT_QUOTES); oci_close($conn); } else { // retrieve the results print "<table cols=5 border=1>\n"; print "<thead>\n"; print "<tr>\n"; print "<th width=120 height=30 align=center> Student ID</th>\n"; print "<th width=150 height=30 align=center> Surname</th>\n"; print "<th width=120 height=30 align=center> Forename</th>\n"; print "<th width=120 height=30 align=center> Module</th>\n"; print "<th width=100 height=30 align=center> Mark</th>\n"; print "</tr>"; print "</thead>"; while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) { echo "<tr>\n"; foreach ($row as $item) { echo " <td align=center>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>\n"; } echo "</tr>\n"; } oci_close($conn); echo "</table>\n"; } ?> |