シンプルに接続
// 基本接続 $dsn ="sqlite:yoga.db"; $pdo = new PDO($dsn); // SQL文処理 $sql = "select * from tb_shop"; $statement = $pdo->prepare($sql); $statement->execute();
ループ(レコード)読み込み
// 基本接続 $dsn ="sqlite:yoga.db"; $pdo = new PDO($dsn); // SQL文処理 $sql = "select * from tb_shop"; $rs = $pdo->prepare($sql); $rs -> execute(); foreach ($rs->fetchall() as $key => $value) { echo '<hr>'; var_dump($value); }
エラーチェックなどを行う場合
// SQLite3 に接続 $dsn ="sqlite:yoga.db"; $pdo = new PDO($dsn); $sqltext = "select * from tb_shop"; $sql = $pdo->prepare($sqltext); try{ if(!$sql->execute()){ echo "Failed(SQL文に問題あり)"; } foreach ($sql->fetchall() as $key => $value) { echo "$key:$value[0]<br/>\n"; } } catch(Exception $e){ echo "Failed:(システムエラー)".$e->getMessage(); }