Web/PHP

PHP + MySQL

WakaraNai 2021. 10. 9. 21:49
728x90
반응형

1. XAMPP에서 Apache 먼저 킨 후 MySQL 켜기

2. MySQL의 Admin 버튼 눌러서 phpMyadmin 접속

 

 

3. 테스트용 데이터베이스 이름 확인하기

 

 

테이블 생성

- htdocs 폴더에 table.php 생성

아래 예시는 testTable 테이블 생성

<?php
// 1. Connect MySQL
// IP, user, password, DB
  $mysqli = mysqli_connect("localhost", "root", "", "test");
  
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
  }
  else {
  
  // 2. Create SQL query
    $sql = "CREATE TABLE testTable (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, testColumn VARCHAR(75))";

 // 3. Run the query
    $result = mysqli_query($mysqli, $sql);
    if ($result === TRUE) {
      echo "Table testTable successfully created";
    } else {
      printf("Could not create table: %s\n", mysqli_connect_error());
    }
    
  //4. Close connection
    mysqli_close($mysqli);
  }
?>

 

 

데이터 추가

- htdocs에 insert_form.html 생성

<!DOCTYPE html>
<html lang="en">

<head>
  <title> Record Insertion Form </title>
</head>

<body>
  <form action="insert.php" method="post">
    <p>Text to Add:<br>
      <input type="text" name="testColumn" size="30">
    </p>
    <p> <input type="submit" name="submit" value="Insert Record"></p>
  </form>
</body>
</html>

 

- htdocs에 insert.php 생성

<?php
// 1. Connect MySQL
// IP, user, password, DB
  $mysqli = mysqli_connect("localhost", "root", "", "test");
  
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
  }
  else {
  
  // 2. Create SQL query
  // . : Concatenation operator
    $sql = "INSERT into testTable (testColumn) values (' ".$_POST["testColumn"]." ')";

 // 3. Run the query
    $result = mysqli_query($mysqli, $sql);
    if ($result === TRUE) {
      echo "A record has been inserted";
    } else {
      printf("Could not insert record: %s\n", mysqli_connect_error());
    }
    
  //4. Close connection
    mysqli_close($mysqli);
  }
?>

 

 

 

 

데이터 총 개수 출력

- htdocs에 count.php 생성

<?php
// 1. Connect MySQL
// IP, user, password, DB
  $mysqli = mysqli_connect("localhost", "root", "", "test");
  
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
  }
  else {
  
  // 2. Create SQL query
  // . : Concatenation operator
    $sql = "SELECT * FROM testTable";

 // 3. Run the query
    $result = mysqli_query($mysqli, $sql);
    if ($result) {
      $num_of_rows = mysqli_num_rows($result);
      printf("Result set has %d rows.\n", $num_of_rows);
    } else {
      printf("Could not retrieve record: %s\n", mysqli_connect_error());
    }
    
    // result에 들어있는 테이블을 청소
    mysqli_free_result($result); 
  //4. Close connection
    mysqli_close($mysqli);
  }
?>

 

 

데이터 내용 출력

- htdocs에 fetch.php 생성

 

  • 특정 컬럼에 접근하고자 할 때 
    • MYSQLI_ASSOC : association 옵션 - 컬럼 이름으로 접근할 수 있도록 지원
    • MYSQLI_NUM : 컬럼 index 숫자로 접근할 수 있도록 지원
    • MYSQLI_BOTH: 둘 다 가능
<?php
  $mysqli = mysqli_connect("localhost", "root", "", "test");
  
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
  }
  else {
    $sql = "SELECT * FROM testTable";

    $result = mysqli_query($mysqli, $sql);
    if ($result) {
     
      // mysqli association
      // trying to get record from table
      // row 수 만큼 출력 반복 : $new_array = 1 row
      while ( $newArray = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $id = $newArray['id'];
        $testColumn = $newArray['testColumn'];
        echo "The ID is ".$id." and the text is ".$testColumn."<br>";
      }

    } else {
      printf("Could not retrieve record: %s\n", mysqli_connect_error());
    }
    
    // result에 들어있는 테이블을 청소
    mysqli_free_result($result); 
    mysqli_close($mysqli);
  }
?>

728x90
반응형

'Web > PHP' 카테고리의 다른 글

[PHP] File Upload  (0) 2021.11.06
SQLite  (0) 2021.10.09
파일 읽고 쓰기 - 간단한 데이터를 다룰 때  (0) 2021.10.09
환경설정 - Apache, PHP  (0) 2021.10.09
PHP 설치 - XAMPP  (0) 2021.10.06