Skip to content

Instantly share code, notes, and snippets.

@taitokiss
Last active November 14, 2016 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taitokiss/aaff31d03f504666d02bb5dcb6006314 to your computer and use it in GitHub Desktop.
Save taitokiss/aaff31d03f504666d02bb5dcb6006314 to your computer and use it in GitHub Desktop.
mysqli_系 データベースへの接続サンプル(手続き型)
<?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