Advertisement
Guest User

Untitled

a guest
Nov 26th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. <?php
  2.  
  3. $dsn = 'mysql:host=localhost;dbname=uriage;charset=utf8';
  4. $user = 'testuser';
  5. $password = 'testuser';
  6.  
  7. // 例外処理
  8. try{ // 例外が発生するおそれがあるコード
  9. // PDOクラスのオブジェクトを作成
  10. $dbh = new PDO($dsn, $user, $password);
  11.  
  12. // データベース操作
  13. $sql = 'select id, name from shouhin';
  14.  
  15. // SELECT文を実行
  16. $sth = $dbh->query($sql);
  17.  
  18. // 連想配列を取得
  19. while($result = $sth->fetch(PDO::FETCH_ASSOC)){
  20. echo "id=" . $result['id'];
  21. echo ",name=" . $result['name'].'<br>';
  22. }
  23.  
  24. // データベースへの接続を閉じる
  25. $dbh = null;
  26.  
  27. }catch (PDOException $e){ // 例外発生時の処理
  28. die("Error:" . $e->getMessage());
  29. }
  30.  
  31. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement