반응형

PHP 데이터베이스와 연동하여 게시물 조회

<?php

$dbConn = mysqli_connect("127.0.0.1", "root", "", "article") or die("MySQL 연결 실패");


// url에 넘어온 파라미터를 받아옴
$id = $_GET["id"];


// 넘어온 값을 이용하여 데이터 조회하는 쿼리문
$sql = "select * from article where id = ".$id;


// $resultSet에 실행 결과 저장
$resultSet = mysqli_query($dbConn, $sql);


// $reslutSet에 담긴 자료 중 첫번째 row를 $detail에 저장
$detail = mysqli_fetch_assoc($resultSet);

?>

 

출력

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

    <?php if ($detail != null) { ?>
    <h1>
        상세페이지
    </h1>
    번호 : <?= $detail['id']?><br>
    작성일자 : <?= $detail['regDate']?><br>
    수정일자 : <?= $detail['updateDate']?><br>
    제목 : <?= $detail['title']?><br>
    <?php }else { ?>
        <div><?=$id?>번째 글은 존재하지 않습니다.</div>
    <?php } ?>    
    <button type="button" onClick="location.href='../list.php'">리스트로</button>

</body>
</html>
반응형

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

[PHP] 세션을 이용한 로그인 기능 구현  (0) 2021.05.31
[PHP] 줄바꿈 처리  (0) 2021.05.26
[PHP] 데이터베이스 연동  (0) 2021.05.24
[PHP] 반복문(for, while, foreach)  (0) 2021.05.24
[PHP] 변수 선언  (0) 2021.05.24

+ Recent posts