메뉴 건너뛰기

프로그램언어

조회 수 8208 추천 수 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. PHP eregi가 빠를까, strpos가 빠를까?

    Date2018.10.27 Views4111
    Read More
  2. php date 날짜 관련 함수

    Date2021.03.27 Views417
    Read More
  3. PHP continue 문

    Date2015.04.14 Views21102
    Read More
  4. PHP - 공공 DATA XML 파싱(PHP 버전)

    Date2023.01.12 Views277
    Read More
  5. PHP + 유튜브(youtube) 동영상 업로드 연동 소스

    Date2021.01.21 Views1153
    Read More
  6. PHP $_SERVER 함수

    Date2019.02.25 Views1569
    Read More
  7. PEAR DB 관련 함수들

    Date2021.03.26 Views694
    Read More
  8. parse_ini_file — Parse a configuration file

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

    Date2018.07.24 Views4819
    Read More
  10. mysql_result — 결과 데이터를 반환

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

    Date2016.12.23 Views18648
    Read More
  12. mysql_insert_id

    Date2016.12.23 Views18759
    Read More
  13. mysql_free_result(); 관련 오류

    Date2021.03.25 Views361
    Read More
  14. mysql_affected_rows — 최근 MySQL 작업으로 변경된 행 개수를 얻음

    Date2016.12.23 Views18817
    Read More
  15. MySQL(MariaDB) 테이블 만들기

    Date2018.03.28 Views8208
    Read More
  16. mysql 에러 구문 표시

    Date2014.02.27 Views20358
    Read More
  17. MYSQL 업데이트 두 번 하기

    Date2014.02.27 Views19737
    Read More
  18. MYSQL DB의 모든 테이블에서 문자열 검색 하기

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

    Date2015.04.14 Views20167
    Read More
  20. MYSQL DB 다중접속을 해결 하는 한 방법

    Date2021.03.26 Views287
    Read More
Board Pagination Prev 1 ... 8 9 10 11 12 13 14 15 16 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved