概要
phpでデータベースからデータを読み込みする記述
※個人学習用メモ・記録
sql.php
<?php
//ここでsqlデータベースやユーザー名、ローカルホストもろもろ取得してるのかな。。
$pdo = new PDO('mysql:host=localhost; dbname=mydb; charset=utf8','root','');
//ここでsql文を叩く
$sql = 'SELECT * FROM jobs';
$statement = $pdo->prepare($sql);
$statement->execute(); // => sqlを実行している
//ここで配列にsqlを押し込んでる
$results = [];
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
$results[] = $row;
}
$statement = null;
$pdo = null;
$message = 'hello world';
require_once 'views/content.tpl.php';
content.tpl.php
<!DOCTYPE html>
<html lang='ja'>
<?php include('header.inc.php'); ?>
<body>
<h1><?= $message ?></h1>
<?php foreach ($results as $player) { ?>
<p><?php print_r($player); ?></p>
<?php } ?>
<?php include('footer.inc.php'); ?>
</body>
</html>