반응형
DB 생성
//mysqli 방식
<?php
$dbName = "testDB";
$tblName = "testTBL";
$conn = mysqli_connect("localhost", "root", "111111");
$query = "create database ${dbName};";
$result = mysqli_query($conn, $query);
?>
//PDO 방식
<?php
try{
$dbName = "testDB";
$tblName = "testTBL";
$conn = new PDO("mysql:host=localhost;", "root", "123456");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "create database ${dbName};";
$stmt = $conn->prepare($query);
$stmt->execute();
}catch(PDOException $e){
echo "Failed: " . $e->getMessage();
}
?>
테이블 생성
//mysqli 방식
<?php
$dbName = "testDB";
$tblName = "testTBL";
$conn = mysqli_connect("localhost", "root", "111111");
mysqli_select_db($conn, $dbName);
$query = "create table ${tblName}(id int(5) not null auto_increment primary key,data varchar(50) not null)";
$result = mysqli_query($conn, $query);
?>
//PDO 방식
<?php
try{
$dbName = "testDB";
$tblName = "testTBL";
$conn = new PDO("mysql:host=localhost;dbname=${dbName}", "root", "123456");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "create table ${tblName}(id int(5) not null auto_increment primary key,data varchar(50) not null)";
$stmt = $conn->prepare($query);
$stmt->execute();
}catch(PDOException $e){
echo "Failed: " . $e->getMessage();
}
?>
반응형
'WEB > PHP' 카테고리의 다른 글
[PHP] MySQL_SELECT : mysqli_fetch_array(); (0) | 2020.09.08 |
---|---|
[PHP] MySQL_SELECT : mysqli_fetch_assoc(); (0) | 2020.09.08 |
[PHP] MySQL_SELECT : mysqli_fetch_row(); (0) | 2020.09.08 |
[PHP] MySQL_INSERT : mysqli, PDO (0) | 2020.09.08 |
[PHP] MySQL 연결하기 : mysqli, PDO (0) | 2020.09.08 |