Skip to content

Instantly share code, notes, and snippets.

@taitokiss
Created November 14, 2016 05:52
Show Gist options
  • Save taitokiss/d7625c6132a5674888e5dace13354a1f to your computer and use it in GitHub Desktop.
Save taitokiss/d7625c6132a5674888e5dace13354a1f to your computer and use it in GitHub Desktop.
mysqli_系 データベースへの接続サンプル5(オブジェクト型)
<?php
// mysqliクラスのオブジェクトを作成
$mysqli = new mysqli("localhost", "testuser", "testuser", "uriage");
if ($mysqli->connect_error) { // connect_errorはPHP5.3.0以降で有効
die("connect_error" . $mysqli->connect_error);
}
// 文字化け防止
$mysqli->set_charset("utf8");
// 完成済みのSELECT文を実行する
if ($result = $mysqli->query("SELECT id, name FROM shouhin WHERE id > 2 AND id < 5")) {
// 連想配列を取得
while ($row = $result->fetch_assoc()) {
echo "id=" . $row["id"];
echo ",name=" . $row["name"] . "<br />";
}
// 結果セットを開放
$result->free();
}
// DB接続を閉じる
$mysqli->close();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment