0
0

More than 3 years have passed since last update.

php データベースからデータ読み込み

Posted at

概要

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>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0