0
0

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 5 years have passed since last update.

PHP For Beginnersチュートリアル その11 データベースからのデータの取得

Posted at

このシリーズの目的

体系的なwebコーディングの訓練ができるようになるためにPHPの初学のきっかけかつ、PHPでログインフォームやフォームを実装することができるようになるために

PHP For Beginners

上記のチュートリアルを進めているのでその備忘録。

前回

内容

今回のチュートリアル

PHP Tutorial - How To Get Data From MySQL Database

このチュートリアルでやること

・データベースからのデータの取得について確認をする

*チュートリアルをやる前に自分でデータベースを予め作っておくこと。
作り方がわからなければこれまでのチュートリアルからの補足からその4へ飛んで確認してほしい。

成果物

index.php

<!DOCTYPE html>
<html lang=ja>
<head>
	<meta charset="UTF-8">
	<title> Getting Data From MySQL Database With PHP</title>
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
	<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>
<body>
	<div class="container">
		<div class="row">
			<div class="col-md-8 offset-md-2">
				<table class="table table-hover table-bordered">
					<tr>
						<td>ID</td>
						<td>Name</td>
						<td>Country</td>
					</tr>
	
					<?php 
					$conn = new mysqli('localhost', 'root' , '', "gettingMySQL" );

					$sql = $conn -> query('SELECT * FROM country');
					 while ($data = $sql -> fetch_array()) {
					 	echo "<tr>
					 	<td>" . $data['id'] . " </td>
					 	<td>" . $data['name'] . " </td>
					 	<td>" . $data['country'] . "</td>
					 	</tr>";
					 	
					 }
				 ?>
				</table>
			</div>
		</div>
	</div>
</body>
</html>

これまでのチュートリアルからの補足

下記チュートリアルでやったことからの補足が今回のチュートリアルである。

PHP For Beginnersチュートリアル その4

PHP For Beginnersチュートリアル その6

fetch_arrayとかqueryってなんだっけとなったらもう一度確認してほしい。
下記の注釈で記載するが補足としては上記のチュートリアルでは取得する値を指定していたが、今回はすべて取得する処理はどうなるかということである。

今回のコードの注釈

query(SELECT......)について

$sql = $conn -> query('SELECT * FROM country');


query関数でSELECTから始まる場合、データベースに登録されているデータの値を取得するためのクエリ文になるということは学習済だが、これまではSELECTに取得したいデータの値を指定していたのに対して今回はアスタリスクを指定することですべての値を取得することができる。
今回は取得した値をテーブルにして表記する形になっている。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?