-
Notifications
You must be signed in to change notification settings - Fork 0
/
CH_7_13_4.php
72 lines (59 loc) · 1.72 KB
/
CH_7_13_4.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* PDOによるプリペアードクエリの実行
* Created by IntelliJ IDEA.
* User: ishitsuka
* Date: 13/04/23
* Time: 19:03
* To change this template use File | Settings | File Templates.
*/
// SQLite3のPDOオブジェクト
$pdo = new PDO('sqlite:/tmp/test.sql3');
// テストテーブル作成
$pdo->exec('CREATE TABLE test (type, int);');
$pdo->exec('INSERT INTO test (type) VALUES (1)');
$pdo->exec('INSERT INTO test (type) VALUES (3)');
// プリペアードクエリ
$sql = 'SELECT * FROM test WHERE type = :type1 OR type = :type2';
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
':type1' => 1,
':type2' => 2));
// 結果の取得
$records = $stmt->fetchAll();
?>
<?php
/**
* 疑問符パラメータ
*/
// SQLite3のPDOオブジェクト
$pdo = new PDO('sqlite:/tmp/test.sql3');
// テストテーブル作成
$pdo->exec('CREATE TABLE test (type, int);');
$pdo->exec('INSERT INTO test (type) VALUES (1);');
$pdo->exec('INSERT INTO test (type) VALUES (3)');
// プリペアードクエリ
$sql = 'SELECT * FROM test WHERE type = ? OR type = ?';
$stmt = $pdo->prepare($sql);
$stmt->execute(array(1, 2));
// 結果の取得
$records = $stmt->fetchAll();
?>
<?php
try {
// SQLite3のPDOオブジェクト
$pdo = new PDO('sqlite:/tmp/test.sql3');
// テストテーブル作成
$pdo->exec('CREATE TABLE test (type, int);');
$pdo->exec('INSERT INTO test (type) VALUES (1);');
$pdo->exec('INSERT INTO test (type) VALUES (3)');
// プリペアードクエリ
$sql = 'SELECT * FROM test WHERE type = ? OR type = ?';
$stmt = $pdo->prepare($sql);
$stmt->execute(array(1, 2));
// 結果の取得
$records = $stmt->fetchAll();
} catch (PDOException $e) {
var_dump($e);
}
?>