Created
May 18, 2012 05:28
-
-
Save ynakajima/2723340 to your computer and use it in GitHub Desktop.
PDOでMySQLに接続する
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 | |
//アカウント設定 | |
$db_host = "db_host"; | |
$db_name = "db_name"; | |
$user_name = "user_name" | |
$user_password = "user_password"; | |
//データベース接続 | |
$db = new PDO('mysql:host=' . $db_host . ';dbname=' . $db_name, $user_name, $user_password); | |
//文字コード設定 | |
$db->query("SET NAMES utf8;"); | |
//SQL文を実行 | |
$stmt = $db->prepare("SELECT * FROM `table_name` WHERE `id` = :ID;"); | |
$stmt->bindParam(":ID", 4); | |
$stmt->execute(); | |
//連想配列で結果を取得 | |
$return = array(); | |
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ | |
$return[$row['id']] = $row; | |
} | |
//DB接続解除 | |
$db = null; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment