Stack/PHP

[PHP] MySQL 연동하기

7ingout 2022. 5. 26. 14:53

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>