try { $pdo_object = new PDO('mysql:host=localhost;dbname=Challenge_db;charset=utf8', 'sakamoto', 2591); } catch (PDOException $E) { die('接続に失敗しました:' . $E->getMessage()); } //SQL文を格納した文字列を定義 $sql = "select * from {$this->table}"; //実行とその結果を受け取れる変数を用意 $query = $pdo_object->prepare($sql); //SQLを実行 $query->execute(); while ($row = $query->fetch(PDO::FETCH_OBJ)) { echo $row->stationID; echo $row->stationName . "<br>"; } } //初期化処理 public function __construct() { $this->table = 'station'; } } $profile = new Human(); $profile->show(); $station = new Station(); $station->show(); $pdo_object = null; ?> </body> </html>
protected function load() { try { $pdo_object = new PDO('mysql:host=localhost;dbname=challenge_db;charset=SJIS', 'wakasa', 'sora2525'); } catch (PDOException $Exception) { die('接続に失敗しました:' . $Exception->getMessage()); } $sql = "select * from {$this->table}"; $query = $pdo_object->prepare($sql); $query->execute(); $data = array(); $data = $query->fetchAll(PDO::FETCH_ASSOC); $pdo_object = null; return $data; } public function show() { $output = $this->load(); foreach ($output as $key1 => $value1) { foreach ($value1 as $key2 => $value2) { echo $value2 . ","; } } } } $aaaa = new Human(); $aaaa->show(); echo "<br/><br/>"; $bbbb = new Station(); $bbbb->show(); //課題1-6