質問編集履歴

5

1

2019/09/08 07:38

投稿

yuntama
yuntama

スコア15

test CHANGED
File without changes
test CHANGED
@@ -10,82 +10,12 @@
10
10
 
11
11
  ```php
12
12
 
13
- $pdo = new PDO($db, $user,$pass,array(PDO::ATTR_EMULATE_PREPARES => false));
14
13
 
15
- $list = $pdo->prepare("SELECT * FROM post WHERE board_id=:board_id ORDER BY id DESC LIMIT 0,10;");
16
-
17
- $list->bindValue(':board_id', $board_id,PDO::PARAM_INT);
18
-
19
- $list->execute();
20
14
 
21
15
  ```
22
16
 
23
17
  ```php
24
18
 
25
- $where = array(
26
19
 
27
- 'key' => 'value',
28
-
29
- 'board_id' => 12,
30
-
31
- 'message' => 2,
32
-
33
- 'password' => 2,
34
-
35
- 'created_at' => 2,
36
-
37
- 'updated_at' => 2,
38
-
39
- );
40
-
41
- function getTable2($pdo, $table, $where = array())
42
-
43
- { $sql = 'SELECT * FROM ' . $table;
44
-
45
- if (!empty($where)) {
46
-
47
- $sql .= ' WHERE';
48
-
49
- $where = '';
50
-
51
- foreach ($where as $k => $v) {
52
-
53
- if ($where !== '')
54
-
55
- $where .= $k . ' = ' . ':' . $k;
56
-
57
- }
58
-
59
- $where .= 'ORDER BY DESC LIMIT 0,10); ';
60
-
61
- } $stmt = $pdo->prepare($sql . $where);
62
-
63
- $where = array(
64
-
65
- 'key' => 'value',
66
-
67
- 'board_id' => 12,
68
-
69
- 'message' => 2,
70
-
71
- 'password' => 2,
72
-
73
- 'created_at' => 2,
74
-
75
- 'updated_at' => 2,
76
-
77
- );
78
-
79
- foreach($where as $k => $v) {
80
-
81
- $stmt->bindValue(':' . $k, $v, PDO::PARAM_STR);
82
-
83
- }
84
-
85
- $stmt->execute();
86
-
87
- return $stmt->fetchAll(PDO::FETCH_ASSOC);
88
-
89
- }
90
20
 
91
21
  ```

4

コード修正

2019/09/08 07:38

投稿

yuntama
yuntama

スコア15

test CHANGED
File without changes
test CHANGED
@@ -42,29 +42,23 @@
42
42
 
43
43
  { $sql = 'SELECT * FROM ' . $table;
44
44
 
45
- $where = '';
45
+ if (!empty($where)) {
46
46
 
47
- if (!empty($where))
47
+ $sql .= ' WHERE';
48
48
 
49
- { $sql .= ' WHERE';
49
+ $where = '';
50
50
 
51
- $where2 = '';
51
+ foreach ($where as $k => $v) {
52
52
 
53
- foreach ($where as $k => $v)
54
-
55
- {
56
-
57
- if ($where2 !== '')
53
+ if ($where !== '')
58
54
 
59
55
  $where .= $k . ' = ' . ':' . $k;
60
56
 
61
57
  }
62
58
 
63
- $where .= ' ORDER BY id DESC LIMIT 0,10);';
59
+ $where .= 'ORDER BY DESC LIMIT 0,10); ';
64
60
 
65
- }
66
-
67
- $list = $pdo->prepare($sql . $where);
61
+ } $stmt = $pdo->prepare($sql . $where);
68
62
 
69
63
  $where = array(
70
64
 
@@ -72,19 +66,25 @@
72
66
 
73
67
  'board_id' => 12,
74
68
 
69
+ 'message' => 2,
70
+
71
+ 'password' => 2,
72
+
73
+ 'created_at' => 2,
74
+
75
+ 'updated_at' => 2,
76
+
75
77
  );
76
78
 
77
- foreach($where as $k => $v)
79
+ foreach($where as $k => $v) {
78
80
 
79
- {
80
-
81
- $list->bindValue(':' . $k,PDO::PARAM_STR);
81
+ $stmt->bindValue(':' . $k, $v, PDO::PARAM_STR);
82
82
 
83
83
  }
84
84
 
85
- $list->execute();
85
+ $stmt->execute();
86
86
 
87
- return $list->fetchAll(PDO::FETCH_ASSOC);
87
+ return $stmt->fetchAll(PDO::FETCH_ASSOC);
88
88
 
89
89
  }
90
90
 

3

コード更新

2019/09/06 04:44

投稿

yuntama
yuntama

スコア15

test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,13 @@
10
10
 
11
11
  ```php
12
12
 
13
+ $pdo = new PDO($db, $user,$pass,array(PDO::ATTR_EMULATE_PREPARES => false));
14
+
13
- $xxx = $pdo->prepare("SELECT * FROM xxx WHERE xxx=:xxx ORDER BY id DESC LIMIT 0,10;");
15
+ $list = $pdo->prepare("SELECT * FROM post WHERE board_id=:board_id ORDER BY id DESC LIMIT 0,10;");
16
+
17
+ $list->bindValue(':board_id', $board_id,PDO::PARAM_INT);
18
+
19
+ $list->execute();
14
20
 
15
21
  ```
16
22
 
@@ -18,25 +24,23 @@
18
24
 
19
25
  $where = array(
20
26
 
21
- 'key' => 'value',
27
+ 'key' => 'value',
22
28
 
23
- 'xx' => 12,
29
+ 'board_id' => 12,
24
30
 
25
- 'xx' => 2,
31
+ 'message' => 2,
26
32
 
27
- 'xx' => 2,
33
+ 'password' => 2,
28
34
 
29
- 'xx' => 2,
35
+ 'created_at' => 2,
30
36
 
31
- 'xx' => 2,
37
+ 'updated_at' => 2,
32
38
 
33
- );
39
+ );
34
-
35
-
36
40
 
37
41
  function getTable2($pdo, $table, $where = array())
38
42
 
39
- { $sql = 'SELECT * FROM ' . $table;
43
+ { $sql = 'SELECT * FROM ' . $table;
40
44
 
41
45
  $where = '';
42
46
 
@@ -44,37 +48,29 @@
44
48
 
45
49
  { $sql .= ' WHERE';
46
50
 
47
- $where = '';
51
+ $where2 = '';
48
52
 
49
53
  foreach ($where as $k => $v)
50
54
 
51
55
  {
52
56
 
53
- if ($where !== '')
57
+ if ($where2 !== '')
54
58
 
55
59
  $where .= $k . ' = ' . ':' . $k;
56
60
 
57
61
  }
58
62
 
59
- $where .= ' ORDER BY id DESC LIMIT 0,10;);';
63
+ $where .= ' ORDER BY id DESC LIMIT 0,10);';
60
64
 
61
65
  }
62
66
 
63
- $stmt = $pdo->prepare($sql . $where);
67
+ $list = $pdo->prepare($sql . $where);
64
68
 
65
69
  $where = array(
66
70
 
67
71
  'key' => 'value',
68
72
 
69
- 'xx' => 12,
73
+ 'board_id' => 12,
70
-
71
- 'xx' => 2,
72
-
73
- 'xx' => 2,
74
-
75
- 'xx' => 2,
76
-
77
- 'xx' => 2,
78
74
 
79
75
  );
80
76
 
@@ -82,13 +78,13 @@
82
78
 
83
79
  {
84
80
 
85
- $stmt->bindValue(':' . $k, $v, PDO::PARAM_STR);
81
+ $list->bindValue(':' . $k,PDO::PARAM_STR);
86
82
 
87
83
  }
88
84
 
89
- $stmt->execute();
85
+ $list->execute();
90
86
 
91
- return $stmt->fetchAll(PDO::FETCH_ASSOC);
87
+ return $list->fetchAll(PDO::FETCH_ASSOC);
92
88
 
93
89
  }
94
90
 

2

コード編集

2019/09/06 04:13

投稿

yuntama
yuntama

スコア15

test CHANGED
File without changes
test CHANGED
@@ -15,6 +15,24 @@
15
15
  ```
16
16
 
17
17
  ```php
18
+
19
+ $where = array(
20
+
21
+ 'key' => 'value',
22
+
23
+ 'xx' => 12,
24
+
25
+ 'xx' => 2,
26
+
27
+ 'xx' => 2,
28
+
29
+ 'xx' => 2,
30
+
31
+ 'xx' => 2,
32
+
33
+ );
34
+
35
+
18
36
 
19
37
  function getTable2($pdo, $table, $where = array())
20
38
 

1

タイトル編集

2019/09/06 03:56

投稿

yuntama
yuntama

スコア15

test CHANGED
@@ -1 +1 @@
1
- ユーザー定義関数の作成
1
+ ユーザー定義関数が上手くいかない
test CHANGED
File without changes