Last active
November 14, 2016 05:31
-
-
Save taitokiss/aaff31d03f504666d02bb5dcb6006314 to your computer and use it in GitHub Desktop.
mysqli_系 データベースへの接続サンプル(手続き型)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// MySQLへ接続 | |
$link = mysqli_connect("localhost", "testuser", "testuser"); | |
if(mysqli_connect_errno() > 0){ | |
die("接続失敗" . mysqli_connect_error()); | |
} | |
// データベースの選択 | |
$db = mysqli_select_db($link, "uriage"); | |
if (!$db){ | |
die("データベースの選択失敗" . mysqli_error()); | |
} | |
// 文字化け防止 | |
mysqli_set_charset($link, "utf8"); | |
// SELECT文の発行 | |
$result = mysqli_query($link, "SELECT id, name FROM shouhin"); | |
if (!$result) { | |
die("クエリーが失敗" . mysqli_error()); | |
} | |
// データの取得及び取得データの表示 | |
while ($row = mysqli_fetch_assoc($result)) { | |
echo "id=" . $row["id"]; | |
echo ",name=" . $row["name"] . "<br />"; | |
} | |
// MySQLの切断 | |
$close = mysqli_close($link); | |
if ($close){ | |
echo "<p>切断成功</p>"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment