메뉴 건너뛰기

프로그램언어

조회 수 8154 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
PHP에서 MySQL(MariaDB) 테이블을 만드는 방법


1. MySQLi Object-oriented 예제
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);


// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";

if ($conn->query($sql) === TRUE) {
    echo "Table MyGuests created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}

$conn->close();
?>


2. MySQLi Procedural 예제
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";

if (mysqli_query($conn, $sql)) {
    echo "Table MyGuests created successfully";
} else {
    echo "Error creating table: " . mysqli_error($conn);
}

mysqli_close($conn);
?>


3. PDO 예제
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // sql to create table
    $sql = "CREATE TABLE MyGuests (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    firstname VARCHAR(30) NOT NULL,
    lastname VARCHAR(30) NOT NULL,
    email VARCHAR(50),
    reg_date TIMESTAMP
    )";

    // use exec() because no results are returned
    $conn->exec($sql);
    echo "Table MyGuests created successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>

  1. MYSQL DB 다중접속을 해결 하는 한 방법

    Date2021.03.26 Views284
    Read More
  2. MySQL DB 중복여부 검사하여 없는 것만 추가

    Date2015.04.14 Views20157
    Read More
  3. MYSQL DB의 모든 테이블에서 문자열 검색 하기

    Date2021.03.26 Views915
    Read More
  4. MYSQL 업데이트 두 번 하기

    Date2014.02.27 Views19729
    Read More
  5. mysql 에러 구문 표시

    Date2014.02.27 Views20349
    Read More
  6. MySQL(MariaDB) 테이블 만들기

    Date2018.03.28 Views8154
    Read More
  7. mysql_affected_rows — 최근 MySQL 작업으로 변경된 행 개수를 얻음

    Date2016.12.23 Views18807
    Read More
  8. mysql_free_result(); 관련 오류

    Date2021.03.25 Views353
    Read More
  9. mysql_insert_id

    Date2016.12.23 Views18752
    Read More
  10. mysql_real_escape_string 이진 데이터를 입력할 경우 이 함수를 사용해야 함

    Date2016.12.23 Views18640
    Read More
  11. mysql_result — 결과 데이터를 반환

    Date2016.12.23 Views18967
    Read More
  12. MySQL테이블의 내용을 엑셀파일(xls)로 다운로드 하기

    Date2018.07.24 Views4798
    Read More
  13. parse_ini_file — Parse a configuration file

    Date2016.12.23 Views19774
    Read More
  14. PEAR DB 관련 함수들

    Date2021.03.26 Views690
    Read More
  15. PHP $_SERVER 함수

    Date2019.02.25 Views1558
    Read More
  16. PHP + 유튜브(youtube) 동영상 업로드 연동 소스

    Date2021.01.21 Views1141
    Read More
  17. PHP - 공공 DATA XML 파싱(PHP 버전)

    Date2023.01.12 Views270
    Read More
  18. PHP continue 문

    Date2015.04.14 Views21097
    Read More
  19. php date 날짜 관련 함수

    Date2021.03.27 Views411
    Read More
  20. PHP eregi가 빠를까, strpos가 빠를까?

    Date2018.10.27 Views4091
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved