private $db;
private $stmt;
public function connect() {
// 데이터베이스 연결
$id = "";
$dsn = "mysql:host=localhost;port=3306;dbname=".$id.";charset=utf8";
try {
$this->db = new PDO($dsn, $id, "", array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo $e->getMessage();
}
}
// 전체조회
public function selectAll($query){
$this->connect();
$this->stmt = $this->db->prepare($query);
$this->stmt->execute();
$this->db->null;
$this->stmt->setFetchMode(PDO::FETCH_ASSOC );
$this->db = null;
return $this->stmt->fetchAll();
}
<?php
$stmt = $conn->selectAll("SELECT * FROM BOARD_BASIC_DATA ORDER BY INSERT_DT");
$cnt = count($stmt);
?>
<table border="1">
<thead>
<tr>
<td>NO</td>
<td>TITLE</td>
<td>WRITER</td>
<td>INSERT_DT</td>
<td>UPDATE_DT</td>
</tr>
</thead>
<tbody>
<?php
foreach($stmt as $key=>$row){
$idx = $cnt-$key;
echo "<tr>";
echo "<td>".$idx."</td>";
echo "<td>".$row['TITLE']."</td>";
echo "<td>".$row['WRITER']."</td>";
echo "<td>".$row['INSERT_DT']."</td>";
echo "<td>".$row['UPDATE_DT']."</td>";
echo "</tr>";
}
?>
</tbody>
</table>