質問編集履歴
5
1
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,43 +4,8 @@
|
|
4
4
|
下記のコードだと昇順表示できるのですが、その下のユーザー定義関数だと昇順化されません。
|
5
5
|
ORDER BYをいれる位置が合っていないのでしょうか。
|
6
6
|
```php
|
7
|
-
|
7
|
+
|
8
|
-
$list = $pdo->prepare("SELECT * FROM post WHERE board_id=:board_id ORDER BY id DESC LIMIT 0,10;");
|
9
|
-
$list->bindValue(':board_id', $board_id,PDO::PARAM_INT);
|
10
|
-
$list->execute();
|
11
8
|
```
|
12
9
|
```php
|
13
|
-
|
10
|
+
|
14
|
-
'key' => 'value',
|
15
|
-
'board_id' => 12,
|
16
|
-
'message' => 2,
|
17
|
-
'password' => 2,
|
18
|
-
'created_at' => 2,
|
19
|
-
'updated_at' => 2,
|
20
|
-
);
|
21
|
-
function getTable2($pdo, $table, $where = array())
|
22
|
-
{ $sql = 'SELECT * FROM ' . $table;
|
23
|
-
if (!empty($where)) {
|
24
|
-
$sql .= ' WHERE';
|
25
|
-
$where = '';
|
26
|
-
foreach ($where as $k => $v) {
|
27
|
-
if ($where !== '')
|
28
|
-
$where .= $k . ' = ' . ':' . $k;
|
29
|
-
}
|
30
|
-
$where .= 'ORDER BY DESC LIMIT 0,10); ';
|
31
|
-
} $stmt = $pdo->prepare($sql . $where);
|
32
|
-
$where = array(
|
33
|
-
'key' => 'value',
|
34
|
-
'board_id' => 12,
|
35
|
-
'message' => 2,
|
36
|
-
'password' => 2,
|
37
|
-
'created_at' => 2,
|
38
|
-
'updated_at' => 2,
|
39
|
-
);
|
40
|
-
foreach($where as $k => $v) {
|
41
|
-
$stmt->bindValue(':' . $k, $v, PDO::PARAM_STR);
|
42
|
-
}
|
43
|
-
$stmt->execute();
|
44
|
-
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
45
|
-
}
|
46
11
|
```
|
4
コード修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -20,27 +20,27 @@
|
|
20
20
|
);
|
21
21
|
function getTable2($pdo, $table, $where = array())
|
22
22
|
{ $sql = 'SELECT * FROM ' . $table;
|
23
|
+
if (!empty($where)) {
|
24
|
+
$sql .= ' WHERE';
|
23
|
-
|
25
|
+
$where = '';
|
24
|
-
if (!empty($where))
|
25
|
-
{ $sql .= ' WHERE';
|
26
|
-
$where2 = '';
|
27
|
-
foreach ($where as $k => $v)
|
26
|
+
foreach ($where as $k => $v) {
|
28
|
-
{
|
29
|
-
if ($
|
27
|
+
if ($where !== '')
|
30
28
|
$where .= $k . ' = ' . ':' . $k;
|
31
29
|
}
|
32
|
-
$where .= '
|
30
|
+
$where .= 'ORDER BY DESC LIMIT 0,10); ';
|
33
|
-
}
|
34
|
-
$
|
31
|
+
} $stmt = $pdo->prepare($sql . $where);
|
35
32
|
$where = array(
|
36
33
|
'key' => 'value',
|
37
34
|
'board_id' => 12,
|
35
|
+
'message' => 2,
|
36
|
+
'password' => 2,
|
37
|
+
'created_at' => 2,
|
38
|
+
'updated_at' => 2,
|
38
39
|
);
|
39
|
-
foreach($where as $k => $v)
|
40
|
+
foreach($where as $k => $v) {
|
40
|
-
{
|
41
|
-
$
|
41
|
+
$stmt->bindValue(':' . $k, $v, PDO::PARAM_STR);
|
42
42
|
}
|
43
|
-
$
|
43
|
+
$stmt->execute();
|
44
|
-
return $
|
44
|
+
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
45
45
|
}
|
46
46
|
```
|
3
コード更新
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,45 +4,43 @@
|
|
4
4
|
下記のコードだと昇順表示できるのですが、その下のユーザー定義関数だと昇順化されません。
|
5
5
|
ORDER BYをいれる位置が合っていないのでしょうか。
|
6
6
|
```php
|
7
|
+
$pdo = new PDO($db, $user,$pass,array(PDO::ATTR_EMULATE_PREPARES => false));
|
7
|
-
$
|
8
|
+
$list = $pdo->prepare("SELECT * FROM post WHERE board_id=:board_id ORDER BY id DESC LIMIT 0,10;");
|
9
|
+
$list->bindValue(':board_id', $board_id,PDO::PARAM_INT);
|
10
|
+
$list->execute();
|
8
11
|
```
|
9
12
|
```php
|
10
13
|
$where = array(
|
11
|
-
|
14
|
+
'key' => 'value',
|
12
|
-
|
15
|
+
'board_id' => 12,
|
13
|
-
|
16
|
+
'message' => 2,
|
14
|
-
|
17
|
+
'password' => 2,
|
15
|
-
|
18
|
+
'created_at' => 2,
|
16
|
-
|
19
|
+
'updated_at' => 2,
|
17
|
-
|
20
|
+
);
|
18
|
-
|
19
21
|
function getTable2($pdo, $table, $where = array())
|
20
|
-
{
|
22
|
+
{ $sql = 'SELECT * FROM ' . $table;
|
21
23
|
$where = '';
|
22
24
|
if (!empty($where))
|
23
25
|
{ $sql .= ' WHERE';
|
24
|
-
$
|
26
|
+
$where2 = '';
|
25
27
|
foreach ($where as $k => $v)
|
26
28
|
{
|
27
|
-
if ($
|
29
|
+
if ($where2 !== '')
|
28
30
|
$where .= $k . ' = ' . ':' . $k;
|
29
31
|
}
|
30
|
-
$where .= ' ORDER BY id DESC LIMIT 0,10
|
32
|
+
$where .= ' ORDER BY id DESC LIMIT 0,10);';
|
31
33
|
}
|
32
|
-
$
|
34
|
+
$list = $pdo->prepare($sql . $where);
|
33
35
|
$where = array(
|
34
36
|
'key' => 'value',
|
35
|
-
'
|
37
|
+
'board_id' => 12,
|
36
|
-
'xx' => 2,
|
37
|
-
'xx' => 2,
|
38
|
-
'xx' => 2,
|
39
|
-
'xx' => 2,
|
40
38
|
);
|
41
39
|
foreach($where as $k => $v)
|
42
40
|
{
|
43
|
-
$
|
41
|
+
$list->bindValue(':' . $k,PDO::PARAM_STR);
|
44
42
|
}
|
45
|
-
$
|
43
|
+
$list->execute();
|
46
|
-
return $
|
44
|
+
return $list->fetchAll(PDO::FETCH_ASSOC);
|
47
45
|
}
|
48
46
|
```
|
2
コード編集
title
CHANGED
File without changes
|
body
CHANGED
@@ -7,6 +7,15 @@
|
|
7
7
|
$xxx = $pdo->prepare("SELECT * FROM xxx WHERE xxx=:xxx ORDER BY id DESC LIMIT 0,10;");
|
8
8
|
```
|
9
9
|
```php
|
10
|
+
$where = array(
|
11
|
+
'key' => 'value',
|
12
|
+
'xx' => 12,
|
13
|
+
'xx' => 2,
|
14
|
+
'xx' => 2,
|
15
|
+
'xx' => 2,
|
16
|
+
'xx' => 2,
|
17
|
+
);
|
18
|
+
|
10
19
|
function getTable2($pdo, $table, $where = array())
|
11
20
|
{ $sql = 'SELECT * FROM ' . $table;
|
12
21
|
$where = '';
|
1
タイトル編集
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
ユーザー定義関数
|
1
|
+
ユーザー定義関数が上手くいかない
|
body
CHANGED
File without changes
|