0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

本日の学習内容Part1

Posted at

参考記事

mpywさんの[PHPでデータベースに接続するときのまとめ]
(https://qiita.com/mpyw/items/b00b72c5c95aac573b71)

DBに接続する

 $db = new PDO('mysql:dbname=mydb; host=127.0.0.1; charset=utf8;', 'root' , '');

mydb-> データベース名
127.0.0.1->ローカルサーバIPアドレス
charset=utf8->文字セット
root->ユーザー名(管理者権限の場合はroot)
'(空)'->パスワードを状況によって設定

##DB接続した時の例外処理

 try{
   $db = new PDO('mysql:dbname=mydb; host=127.0.0.1; charset=utf8;', 'root' , '');
} catch(PDOException $e) {
 echo 'DB接続エラー:' . $e->getMessage();
}

##感想
try~catch句はなかなか使う機会が多そうなので、使いこなせるようにポートフォリオ作りにも積極的に使っていこうと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?