Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $dsn = 'mysql:host=localhost;dbname=uriage;charset=utf8';
- $user = 'testuser';
- $password = 'testuser';
- // 例外処理
- try{ // 例外が発生するおそれがあるコード
- // PDOクラスのオブジェクトを作成
- $dbh = new PDO($dsn, $user, $password);
- // データベース操作
- $sql = 'select id, name from shouhin';
- // SELECT文を実行
- $sth = $dbh->query($sql);
- // 連想配列を取得
- while($result = $sth->fetch(PDO::FETCH_ASSOC)){
- echo "id=" . $result['id'];
- echo ",name=" . $result['name'].'<br>';
- }
- // データベースへの接続を閉じる
- $dbh = null;
- }catch (PDOException $e){ // 例外発生時の処理
- die("Error:" . $e->getMessage());
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement