2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

連想配列について

Last updated at Posted at 2021-04-15

初心者の方は連想配列(辞書的配列)を学ぶと「どこで使うんだろう」と思われる方が多いと思います。
今回はPHPを例にどこで使われているかの一例を紹介いたします。

<?php
	// データベースからデータを取得する
	$sql = "SELECT id,date,text FROM mental ORDER BY id desc;";
	$stmt = $pdo->prepare($sql);
	$stmt -> execute();
	?>
	<table>
		<tr>
			<th>id</th>
			<th>日時</th>
			<th>メンタルの状態</th>
		</tr>
		<!-- HTMLテーブルの2行目以降で全てのレコードを表示 -->
        <?php while ($row = $stmt -> fetch(PDO::FETCH_ASSOC)) { ?>
            <tr>
                <td><?= $row["id"] ?></td>
                <td><?= $row["date"] ?></td>
                <td><?= $row["text"] ?></td>
            </tr>
        <?php } ?>

今回のサンプルは例外処理部分をカットしたものを掲載しています。実際作成したものは例外処理が入っています。こんな感じで使います。

SELECT文のカラム名が「*」だったのでカラム名をコーディングするように変更いたしました。

2
2
2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?