Skip to content

Instantly share code, notes, and snippets.

@taitokiss
Last active November 14, 2016 05:28
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/76dc47f775b2da85a4dd62a21f72513e to your computer and use it in GitHub Desktop.
Save taitokiss/76dc47f775b2da85a4dd62a21f72513e to your computer and use it in GitHub Desktop.
mysqli_系 データベースへの接続サンプル3(手続き型でプリペアドステートメントを使う)
<?php
// MySQLへ接続及びデータベースの選択
$link = mysqli_connect("localhost", "testuser", "testuser", "uriage");
if(mysqli_connect_errno() > 0){
die("接続失敗" . mysqli_connect_error());
}
// 文字化け防止
mysqli_set_charset($link, "utf8");
// プリペアドステートメントを作成
if ($stmt = mysqli_prepare($link, "SELECT id, name FROM shouhin WHERE id >= ?")) {
// マーカにパラメータをバインド
mysqli_stmt_bind_param($stmt, "i", $id);
// パラメータの設定
$id = 3;
// クエリを実行
mysqli_stmt_execute($stmt);
// 結果変数をバインド
mysqli_stmt_bind_result($stmt, $id, $name);
// データの取得及び取得データの表示
while (mysqli_stmt_fetch($stmt)) {
echo "id=" . $id;
echo ",name=" . $name . "<br />";
}
// ステートメントを閉じる
mysqli_stmt_close($stmt);
}
// 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