<?php
// DB 접속 설정
$mysqli = new mysqli("localhost", "db_user", "db_pass", "bodonews_com");
// 에러 확인
if ($mysqli->connect_error) {
die("DB 연결 실패: " . $mysqli->connect_error);
}
// 마지막 uid 값 (첫 페이지면 null)
$last_uid = isset($_GET['last_uid']) ? (int)$_GET['last_uid'] : 0;
$page_size = 30;
if ($last_uid > 0) {
$stmt = $mysqli->prepare("
SELECT uid, name, email, title, wdate
FROM ins_news
WHERE uid < ?
ORDER BY uid DESC
LIMIT ?
");
$stmt->bind_param("ii", $last_uid, $page_size);
} else {
$stmt = $mysqli->prepare("
SELECT uid, name, email, title, wdate
FROM ins_news
ORDER BY uid DESC
LIMIT ?
");
$stmt->bind_param("i", $page_size);
}
$stmt->execute();
$result = $stmt->get_result();
// 결과 출력 (예: HTML 표로 출력)
echo "<table border='1'>";
echo "<tr><th>UID</th><th>Name</th><th>Email</th><th>Title</th><th>Date</th></tr>";
$last_uid_on_page = 0;
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>{$row['uid']}</td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['email']}</td>";
echo "<td>{$row['title']}</td>";
echo "<td>{$row['wdate']}</td>";
echo "</tr>";
$last_uid_on_page = $row['uid'];
}
echo "</table>";
// 다음 페이지 링크
if ($last_uid_on_page > 0) {
echo "<a href='?last_uid={$last_uid_on_page}'>다음 페이지 ▶</a>";
}
$st
==============
<?php
$mysqli = new mysqli("localhost", "db_user", "db_pass", "bodonews_com");
if ($mysqli->connect_error) {
die("DB 연결 실패: " . $mysqli->connect_error);
}
$page_size = 30;
// 전체 게시물 수 및 페이지 수 계산
$result = $mysqli->query("SELECT COUNT(*) as total FROM ins_news");
$total_row = $result->fetch_assoc();
$total_count = (int)$total_row['total'];
$total_pages = ceil($total_count / $page_size);
// 현재 페이지 번호
$page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1;
if ($page > $total_pages) $page = $total_pages;
// 시작 offset 계산 후 해당 uid 찾기
$offset = ($page - 1) * $page_size;
$start_uid_result = $mysqli->query("
SELECT uid FROM ins_news
ORDER BY uid DESC
LIMIT $offset, 1
");
$start_uid_row = $start_uid_result->fetch_assoc();
$start_uid = $start_uid_row ? (int)$start_uid_row['uid'] : PHP_INT_MAX;
// 커서 기반으로 데이터 가져오기
$stmt = $mysqli->prepare("
SELECT uid, name, email, title, wdate
FROM ins_news
WHERE uid <= ?
ORDER BY uid DESC
LIMIT ?
");
$stmt->bind_param("ii", $start_uid, $page_size);
$stmt->execute();
$result = $stmt->get_result();
// 테이블 출력
echo "<table border='1'>";
echo "<tr><th>UID</th><th>Name</th><th>Email</th><th>Title</th><th>Date</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['uid']}</td>
<td>{$row['name']}</td>
<td>{$row['email']}</td>
<td>{$row['title']}</td>
<td>{$row['wdate']}</td>
</tr>";
}
echo "</table>";
// 하단 페이지 링크 출력
echo "<div style='margin-top:10px;'>";
if ($page > 1) {
echo "<a href='?page=" . ($page - 1) . "'>◀ 이전</a> ";
}
$display_pages = 5;
$start = max(1, $page - floor($display_pages / 2));
$end = min($total_pages, $start + $display_pages - 1);
for ($i = $start; $i <= $end; $i++) {
if ($i == $page) {
echo "<strong>[$i]</strong> ";
} else {
echo "<a href='?page=$i'>[$i]</a> ";
}
}
if ($page < $total_pages) {
echo "<a href='?page=" . ($page + 1) . "'>다음 ▶</a>";
}
echo "</div>";
$stmt->close();
$mysqli->close();
?>