Created
November 14, 2016 05:52
-
-
Save taitokiss/d7625c6132a5674888e5dace13354a1f to your computer and use it in GitHub Desktop.
mysqli_系 データベースへの接続サンプル5(オブジェクト型)
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 | |
// 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