본문 바로가기
php/개인공부

mysqli// select 한개만 조회

by SEOKIHOUSE 2023. 10. 31.

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
  <style>
		.box {border: 2px solid black; margin-bottom: 20px;}
		.content {height:300px;}
		.span {margin-right: 20px;}
  </style>
  <script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
 </head>
 <body>
	<h1>상세보기</h1>

	<?php
		$servername = "localhost";
		$username = "root";
		$password = "1234";
		$dbname = "practice";

		$conn = new mysqli($servername, $username, $password, $dbname);

		if($conn->connect_error) {
			die("연결 실패: " . $conn->connect_error);
		}

		$getPageNum = $_GET['p'];
		$getPostNum = $_GET['num'];


		echo "넘어온 페이지 번호: $getPostNum<br>";
		
		$sql = "SELECT * FROM board WHERE num = ?";
		$stmt = $conn->prepare($sql);
		$stmt->bind_param("i", $getPostNum);
		$stmt->execute();
		$result = $stmt->get_result();
		

		//한개씩 가져온다 여러개 가져오면 while만 붙이면됨!!
		if($list = $result->fetch_assoc()) {
			$title = $list['title'];
			$content = $list['content'];
			$writer = $list['writer'];
			$regdate = $list['regdate'];
			$viewCount = $list['viewCount'];
		}
		
	?>

	<div>
		<div class="box">
			<p>
				<span class="span">제목 : <?=$title?></span>	
				<span class="span">등록일 : <?=$regdate?></span>
				<span class="span">조회수 :<?=$viewCount?></span>
			</p>
		</div>
		<div class="box content">
			<p>내용 : <?=$content?></p>
		</div>

		<button type="button" id="list" style="text-align:right;">목록</button>
		<script>
			//원래 페이지로 돌아가기(목록버튼)
			$('#list').click(function() {
				location.href ="board.php?p=<? echo $getPageNum?>";
			});
		</script>
	</div>
	
 </body>
</html>