[PHP] MySQL 연동하기

2022. 5. 26. 14:53·Stack/PHP

MySQL 연동

1. mysqli_connect("호스트주소", ("관리자"), "데이터베이스 아이디", "데이터베이스 비밀번호", "데이터베이스명");

ex> mysqli_connect("localhost", "root", "0000", "test");

 

2. mysqli_query("컨넥트", "쿼리문")

ex> mysqli_query($db, "insert into members(name, addr) values('효동', '울산시');");

 

mysqli_num_rows($result);   // 조회한 결과의 레코드 개수

mysqli_fetch_row($result);

mysqli_fetch_array($result);

mysqli_fetch_assoc($result);

 

 

아파치서버 실행 후!

 

ex09_mysqli.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
        echo "Mysql연결테스트<br>";
        // mysqli_connect("호스트주소", "관리자", "아이디", "비밀번호", "데이터베이스명");
        $db = mysqli_connect("localhost", "root", "0000", "test");
            if($db) {
                echo "성공<br>";
            } else {
                echo "실패<br>";
            }
            $query = "insert into members(name, tel, addr, license) values('효동동', '010-1111-5555', '울산시 울주군', 'y');";
            $result = mysqli_query($db, $query);
            if($result) {
                echo "전송되었습니다.<br>";
            } else {
                echo "전송이 되지않았습니다.<br>";
            }
    ?>
</body>
</html>

 

 

 

ex10_select.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
        $conn = mysqli_connect('localhost', 'root', '0000', 'test');
        echo "<h1>한줄 조회하기</h1>";

        // 쿼리문 작성 후 sql에 할당
        $sql = "select * from members where no = 3";
        $result = mysqli_query($conn, $sql);

        // php에서 사용가능한 데이터 형태인 배열로 반환
        $row = mysqli_fetch_array($result);
        var_dump($row);
        echo "<p>{$row['name']}</p>";
        echo "<p>{$row['addr']}</p>";

        // 여러줄 select
        $sqlMul = "select * from members;";
        $result2 = mysqli_query($conn, $sqlMul);
        echo "<p>여기서부터 전체 조회</p>";
        while($row2 = mysqli_fetch_array($result2)){
            echo "<p>{$row2['addr']}</p>";
        }
    ?>
</body>
</html>

 

ex10_select.php_수정

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
        $conn = mysqli_connect('localhost', 'root', '0000', 'test');
        echo "<h1>한줄 조회하기</h1>";

        // 쿼리문 작성 후 sql에 할당
        $sql = "select * from books where id = 3";
        $result = mysqli_query($conn, $sql);

        // php에서 사용가능한 데이터 형태인 배열로 반환
        // mysqli_fetch_array
        // mysqli_fetch_row
        // mysqli_fetch_assoc
        $row = mysqli_fetch_array($result);
        $result = mysqli_query($conn, $sql);
        $row2 = mysqli_fetch_row($result);
        $result = mysqli_query($conn, $sql);
        $row3 = mysqli_fetch_assoc($result);
        var_dump($row);
        echo "<br/>";
        var_dump($row2);
        echo "<br/>";
        var_dump($row3);
        echo "<br/>";
        echo "<p>{$row['title']}</p>";
        echo "<p>{$row['writer']}</p>";

        // 여러줄 select
        $sqlMul = "select * from books;";
        $result2 = mysqli_query($conn, $sqlMul);
        $total = mysqli_num_rows($result2);
        echo "전체 레코드는 ${total} 이다.";
        echo "<p>여기서부터 전체 조회</p>";
        while($row2 = mysqli_fetch_array($result2)){
            echo "<p>{$row2['title']}</p>";
        }
    ?>
</body>
</html>

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

[PHP] 이미지 업로드  (0) 2022.06.02
[PHP] 쿠키 / 세션  (0) 2022.05.30
[PHP] file 관련 함수를 이용한 Blog  (0) 2022.05.26
[PHP] empty() / isset() / include  (0) 2022.05.26
[PHP] file  (0) 2022.05.25
'Stack/PHP' 카테고리의 다른 글
  • [PHP] 이미지 업로드
  • [PHP] 쿠키 / 세션
  • [PHP] file 관련 함수를 이용한 Blog
  • [PHP] empty() / isset() / include
7ingout
7ingout
  • 7ingout
    Hello, 7ingout world!
    7ingout
  • 전체
    오늘
    어제
    • 분류 전체보기 (205)
      • Project (5)
      • Stack (173)
        • React (40)
        • JavaScript (50)
        • TypeScript (14)
        • HTML (11)
        • CSS (31)
        • Spring (9)
        • PHP (15)
        • SQL (3)
        • Python (0)
      • ETC (9)
      • Design (13)
        • Illustrator (6)
        • Photoshop (7)
      • Articloid (4)
        • 7ingout (4)
  • 공지사항

    • ☻
  • 인기 글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
7ingout
[PHP] MySQL 연동하기
상단으로

티스토리툴바