我对 OOP 和 PDO 还很陌生,并且无法弄清楚如何按页面显示 MYSQL 结果(例如每页 10 个)。最好的方法是什么?
public function getResults() {
try {
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT * FROM table ORDER BY id DESC LIMIT 10";
$result = $con->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$msg_id = $row['id'];
echo '<div id="results">' .$msg_id. '</div>';
}
$con = null;
}catch(PDOException $e) {
echo $e->getMessage();
}
}
}