phpでMySQLに接続, sql文にてデータを挿入する書き方例
$stmt->executeにて実行しています。変数の$dbhはdbhandlerの略です。
try {
$dbh = new PDO('mysql:host='/DB_HOST.';dbname='.DB_NAME,DB_USER,DB_PASSWORD);
} else (PDOException $e){
echo $e->getMessage();
exit;
}
$stmt = $dbh->prepare("select * from users where instagram_user_id=:user_id limit 1");
$stmt->execute(array(":user_id"=>$json->user->id));
$user = $stmt->fetch();
if (empty($user)){
$stmt = $dbh->prepare("insert into users(instagram_user_id, instagram_user_name, instagram_profile_picture, instagram_access_token, created, modified) values (:user_id, :user_name, :profile_picture, :access_token, now(), now());");
$pramas = array(
":user_id" =>$json->user->id,
":user_name" =>$json->user->username,
":profile_picture" =>$json->user->profile_picture,
":access_token"=>$json->access_token
);
$stmt->execute($params);
データの取得
$stmt = $dbh->prepare("select * from users where id=:last_insert_id limit 1");
$stmt->execute(array(":last_insert_id"=>$dbh->lastInsertId()));
$user = $stmt->fetch();