[2주차 과제]php,DB연동

CHIKA·2024년 4월 30일

웹개발

목록 보기
2/9

🟢URL

http://아이피주소:[포트번호]/phpmyadmin/

**phpmyadmin 뒤에 '/' 넣어줘야함!

🟢테이블 만들기

📢error
http://아이피주소:80/score1.php 의 URL로 접속했더니
php가 실행되지 않고 다운로드되는 문제가 발생.
http://아이피주소:1018/score1.php 로 접속했더니 정상적으로 실행된다.
왜인지는 잘...모른다

🟢score.html

🟢score.php
업로드중..

🟢score1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="style.css">
    <link
      rel="stylesheet"
      href="https://cdn.jsdelivr.net/npm/reset-css@4.0.1/reset.min.css"
    />
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
        <form action="score1.php">
        <p><input type="text" name="name" placeholder="이름을 입력하세요"></p>
        <p><input type="submit"></p>
        </form>
</body>
</html>

🟢score1.php

<?php
$conn = mysqli_connect('localhost','admin','student1234','test');
/*
if ($conn) {
    echo "DB Connect OK";
} else {
    echo "DB Connect Fail";
}
*/
if (isset($_GET['name'])) {
    $name = $_GET['name'];

    // 이름에 해당하는 점수 조회
    $query = "SELECT score FROM test_table WHERE name = '$name'";
    $result = mysqli_query($conn, $query);

    // 쿼리 결과 확인
    if (mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result);
        $score = $row['score'];
        echo "$name 님의 점수는 $score 입니다";
    } else {
        echo "해당 이름에 대한 정보를 찾을 수 없습니다.";
    }
} else {
    echo "이름을 입력하세요.";
}

mysqli_close($conn);
?>

🟢함수 설명
mysqli_num_rows() :
MySQL 데이터베이스에서 쿼리로 반환된 결과 세트의 행 수를 반환

mysqli_fetch_assoc() :
MySQL 데이터베이스에서 쿼리로 반환된 결과 세트에서 다음 행을 연관 배열로 반환

0개의 댓글