PHP久しいので近いうち復習する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php ini_set('display_errors', "On"); ini_set('error_reporting', E_ALL); ExecuteQuery("select * from test_table"); function Connection(){ try{ return new PDO("mysql:host=localhost;dbname=test_db;charset=utf8","usr","pwd"); } catch (PDOException $e) { echo $e->getMessage(); exit; } } function ExecuteQuery($sql){ foreach(Connection()->query($sql) as $row){ $result[]=array( 'id'=>$row['id'], 'name'=>$row['name'], 'age'=>$row['age'] ); } header('Content-type: application/json'); echo json_encode($result, JSON_UNESCAPED_UNICODE); } |