[PHP] Oracle, MySQL - PDO 사용방법
간결하게 요약해서 작성해보았다.
1. Oracle, MySQL 데이터베이스 설계하기
데이터베이스를 하나 생성한다.
Member를 생성한다.
그림 1) Oracle SQL Developer에서 Member 테이블 생성하기(Oracle 18c Express Edition)
그림 2) HeidiSQL에서 Member 테이블 생성하기(MySQL)
CREATE OR REPLACE EDITIONABLE TRIGGER "C##RABBITSUN2"."MEMBER_TRG" |
Oracle SQL |
CREATE TABLE `member` ( |
MySQL SQL |
2. Oracle, MySQL - PDO 사용방법(소스코드)
/*
* Subject: PDO
* Created Date: 2019-12-10
* Author by: 도도(Dodo) / rabbitsun2@gmail.com
* Description:
*
* Reference:
*
*/
include 'MySQLDB.php';
include 'OracleDB.php';
$orcl = new OracleDB('127.0.0.1', 'c##rabbitsun2', '1234');
$orcl->connect();
// SELECT 문(OracleDB)
$query = "SELECT * FROM MEMBER";
$list = $orcl->select($query);
/*
foreach($list as $row){
echo $row["ID"] . "/";
echo $row["SUBJECT"] . "<br>";
}
*/
// INSERT 문(OracleDB)
//$subject = '연습';
//$result = $orcl->insert($subject);
$mysqlDB = new MySQLDB('127.0.0.1', 'rabbit2me', '1234', 'rabbit2me');
$mysqlDB->connect();
$list = $mysqlDB->select($query);
foreach($list as $row){
echo $row["id"] . "/";
echo $row["subject"] . "<br>";
}
$subject = '연습';
$result = $mysqlDB->insert($subject);
?>
[소스코드: index.php]
class MySQLDB{
private $hostName;
private $userName;
private $passwd;
private $tns;
private $dbh;
public function __construct($hostName, $userName, $passwd, $dbname){
$this->tns = "mysql:host=$hostName;dbname=$dbname";
$this->hostName = $hostName;
$this->userName = $userName;
$this->passwd = $passwd;
}
public function __destruct(){
unset($this->dbh);
}
public function connect(){
$tns = $this->tns;
$db_username = $this->userName;
$db_password = $this->passwd;
try{
$this->dbh = new PDO($this->tns, $db_username, $db_password,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
//echo "참";
}catch(PDOException $e){
echo ($e->getMessage());
}
}
public function select($query){
$stmt = NULL;
$dbh = $this->dbh;
if ( $dbh != NULL ){
$stmt = $dbh->prepare($query);
$stmt->execute();
$list = $stmt->fetchAll();
return $list;
}
return NULL;
}
public function insert($subject){
$stmt = NULL;
$dbh = $this->dbh;
$query = "INSERT INTO member(subject) VALUES(:subject)";
if ( $dbh != NULL ){
$stmt = $dbh->prepare($query);
$stmt->bindParam(':subject', $subject);
$stmt->execute();
return true;
}
return false;
}
}
?>
[소스코드: MySQLDB.php]
class OracleDB{
private $hostName;
private $userName;
private $passwd;
private $tns;
private $dbh;
public function __construct($hostName, $userName, $passwd){
$this->tns = "(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = $hostName)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = xe)
)
)";
$this->hostName = $hostName;
$this->userName = $userName;
$this->passwd = $passwd;
$this->dbh = NULL;
}
public function __destruct(){
unset($this->dbh);
}
public function connect(){
$tns = $this->tns;
$db_username = $this->userName;
$db_password = $this->passwd;
try{
$this->dbh = new PDO("oci:dbname=". $tns . ";charset=UTF8", $db_username, $db_password);
//echo "참";
}catch(PDOException $e){
echo ($e->getMessage());
}
}
public function select($query){
$stmt = NULL;
$dbh = $this->dbh;
if ( $dbh != NULL ){
$stmt = $dbh->prepare($query);
$stmt->execute();
$list = $stmt->fetchAll();
return $list;
}
return NULL;
}
public function insert($subject){
$stmt = NULL;
$dbh = $this->dbh;
$query = "INSERT INTO MEMBER(SUBJECT) VALUES(:subject)";
if ( $dbh != NULL ){
$stmt = $dbh->prepare($query);
$stmt->bindParam(':subject', $subject);
$stmt->execute();
return true;
}
return false;
}
}
?>
[소스코드: OracleDB.php]
'소프트웨어(SW) > PHP' 카테고리의 다른 글
[PHP] 리눅스, PHP 7.3(8.0) - CSS폰트, 자바스크립트, 서버URL - 공통영역 배포도구(1/3) (173) | 2021.04.07 |
---|---|
[PHP] PHP 7.3, jQuery에서 바코드 생성하기 (CSV파일) (6) | 2021.04.01 |
[PHP] xampp v3.2.4에서 Oracle-PDO, MySQLi 등 설정하기 (2) | 2019.12.10 |
[PHP] Apache 2.4, PHP 7, Oracle 18c Express Edition 윈도우 환경에서 연동하기 (3) | 2019.12.10 |
[PHP] OTP와 로그 시스템 (OTP and Log System) (1) | 2018.08.29 |