Last active
December 13, 2020 09:31
-
-
Save katsube/f47a6bb3b12b39d92d84d20955e633d8 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* MySQLに接続しデータを取得する | |
* | |
*/ | |
//------------------------------------------------- | |
//準備 | |
//------------------------------------------------- | |
$dsn = 'mysql:dbname=rpgdb;host=localhost'; //接続先 | |
$user = 'senpai'; //MySQLのユーザーID | |
$pw = 'indocurry'; //MySQLのパスワード | |
// 実行したいSQL | |
$sql = 'SELECT * FROM Monster'; | |
//------------------------------------------------- | |
//SQLを実行 | |
//------------------------------------------------- | |
$dbh = new PDO($dsn, $user, $pw); //接続 | |
$sth = $dbh->prepare($sql); //SQL準備 | |
$sth->execute(); //実行 | |
//取得した内容を表示する | |
while(true){ | |
//ここで1レコード取得 | |
$buff = $sth->fetch(PDO::FETCH_ASSOC); | |
if( $buff === false ){ | |
break; //データがもう存在しない場合はループを抜ける | |
} | |
// 表示 | |
printf("%d: %s\n", $buff['id'], $buff['name']); //$buffには連想配列として格納されている | |
}; |
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 | |
/** | |
* MySQLに接続しデータを追加する | |
* | |
*/ | |
//------------------------------------------------- | |
//準備 | |
//------------------------------------------------- | |
$dsn = 'mysql:dbname=rpgdb;host=localhost'; //接続先 | |
$user = 'senpai'; //MySQLのユーザーID | |
$pw = 'indocurry'; //MySQLのパスワード | |
// 実行したいSQL | |
$sql = 'INSERT INTO Monster(id, name) VALUES(?,?)'; | |
//------------------------------------------------- | |
//SQLを実行 | |
//------------------------------------------------- | |
$dbh = new PDO($dsn, $user, $pw); //接続 | |
$sth = $dbh->prepare($sql); //SQL準備 | |
$sth->execute(['101', 'baaaaaa']); //実行 |
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 | |
/** | |
* MySQLに接続しデータを追加する | |
* (bindValueを使用する) | |
*/ | |
//------------------------------------------------- | |
//準備 | |
//------------------------------------------------- | |
$dsn = 'mysql:dbname=rpgdb;host=localhost'; //接続先 | |
$user = 'senpai'; //MySQLのユーザーID | |
$pw = 'indocurry'; //MySQLのパスワード | |
// 実行したいSQL | |
$sql = 'INSERT INTO Monster(id, name) VALUES(:id, :name)'; | |
//------------------------------------------------- | |
//SQLを実行 | |
//------------------------------------------------- | |
$dbh = new PDO($dsn, $user, $pw); //接続 | |
$sth = $dbh->prepare($sql); //SQL準備 | |
//プレースホルダに値を入れる | |
$sth->bindValue(':id', 100, PDO::PARAM_INT); | |
$sth->bindValue(':name', 'fooo', PDO::PARAM_STR); | |
// 実行 | |
$sth->execute(); | |
// 上記以外の型は以下を参照 | |
// http://php.net/manual/ja/pdo.constants.php |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment