質問編集履歴

2

誤字を修正

2022/09/28 01:02

投稿

ITOMO5963
ITOMO5963

スコア98

test CHANGED
File without changes
test CHANGED
@@ -16,7 +16,7 @@
16
16
  ### 流したいSQL(例)
17
17
 
18
18
  ```
19
- select hostid from tbl where hostid in (1001, 1002) AND col between '2022-07-05 00:00' AND '2022-09-27 13:00';
19
+ select * from tbl where hostid in (1005, 1006) AND col between '2022-07-05 00:00' AND '2022-09-27 13:00';
20
20
  ```
21
21
 
22
22
  ### 検証用コード
@@ -38,7 +38,7 @@
38
38
 
39
39
  try {
40
40
 
41
- // 過去1週間前の日付取得
41
+ // 過去週間前の日付取得
42
42
  $startDateTime = new DateTime('-12 weeks');
43
43
  $start_date = $startDateTime->format('Y-m-d 00:00');
44
44
 

1

コードを修正しました。

2022/09/28 00:58

投稿

ITOMO5963
ITOMO5963

スコア98

test CHANGED
File without changes
test CHANGED
@@ -3,6 +3,7 @@
3
3
  PHPのPDOで複数のhostidのデータを WHERE IN を使いたいです。
4
4
  hostidの数は可変です。
5
5
  参考URLをもとに作成してみましたが、うまく動作しません。
6
+ executeの引数を3つ実行できないのでしょうか?
6
7
  ご教示お願いいたします。
7
8
 
8
9
  ### 前提
@@ -34,63 +35,33 @@
34
35
 
35
36
  ```
36
37
  <?php
37
- class BASE_INFO{
38
- private $host = 'localhost';
39
- private $db_user = 'dbuser';
40
- private $password = 'hogehoge';
41
- private $db = 'base_info';
42
- private $pdo;
43
- private $dsn;
44
- private $driver_options = [
45
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
46
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
47
- ];
48
38
 
49
- /**
50
- *DBへ接続する
51
- */
52
- function __construct(){
53
- $this->dsn = "mysql:host=$this->host;dbname=$this->db;charset=utf8";
54
- try{
39
+ try {
55
- $this->pdo = new PDO( $this->dsn, $this->db_user, $this->password, $this->driver_options );
56
- }catch(PDOException $e){
57
- error_log('DB Connection failed:' .$e->getMessage() ."\n", 0);
58
- exit();
59
- }
60
- }
61
40
 
41
+ // 過去1週間前の日付取得
42
+ $startDateTime = new DateTime('-12 weeks');
43
+ $start_date = $startDateTime->format('Y-m-d 00:00');
62
44
 
45
+ // 現在時刻取得
46
+ $endDateTime = new DateTime('now');
63
- function Hostid_Check(string $start_date, string $end_date){
47
+ $end_date = $endDateTime->format('Y-m-d H:i');
64
- $id = array(1001, 1002);
65
48
 
49
+ $user_id = '1005,1006';
66
- $inClause = substr(str_repeat(',?', count($names)), 1);
50
+ $user_ids = explode(',', $user_id);
51
+ $dbh = new PDO('mysql:host=localhost;dbname=base_info', 'dbuser', 'Paaword1!');
67
52
 
53
+ //
54
+ $sql = "select * from tbl where col BETWEEN $start_date AND $end_date";
55
+ $sql .= 'AND hostid IN (' . substr(str_repeat(',?', count($user_ids)), 1) . ')';
68
- $stmt = $pdo->prepare(sptinrf('
56
+ $stmt = $dbh->prepare($sql);
69
- select hostid from tbl where hostid in (%d)
70
- and col between (%s) and (%s)
71
- '), $inClause, $start_date, $end_date);
57
+ $stmt->execute($user_ids, $start_date, $end_date);
72
58
 
73
- $stmt->execute($names);
74
- $res = $stmt->fetchAll();
59
+ var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
75
- var_dump($res);
60
+ } catch (Exception $e) {
76
- }
61
+ print "error!! " . $e->getMessage() . PHP_EOL;
77
62
  }
63
+ $dbh = null;
78
64
 
79
- function Func_Exec(){
80
- $base_info = new BASE_INFO();
81
- // 過去〇週間前の日付取得
82
- $startDateTime = new DateTime('-12 weeks');
83
- $start_date = $startDateTime->format('Y-m-d 00:00');
84
- // 現在時刻取得
85
- $endDateTime = new DateTime('now');
86
- $end_date = $endDateTime->format('Y-m-d H:i');
87
-
88
- $base_info->Hostid_Check($start_date, $end_date);
89
-
90
- }
91
-
92
- // 関数実行
93
- Func_Exec();
94
65
  ?>
95
66
  ```
96
67
 
@@ -100,15 +71,9 @@
100
71
  ### エラーメッセージ
101
72
 
102
73
  ```
103
- PHP Notice: Undefined variable: names in /var/www/html/waiis/model/test4.php on line 40
104
- PHP Warning: count(): Parameter must be an array or an object that implements Countable in /var/www/html/waiis/model/test4.php on line 40
74
+ PHP Warning: PDOStatement::execute() expects at most 1 parameter, 3 given in /var/www/html/waiis/model/test5.php on line 21
105
- PHP Notice: Undefined variable: pdo in /var/www/html/waiis/model/test4.php on line 42
106
- PHP Fatal error: Uncaught Error: Call to a member function prepare() on null in /var/www/html/waiis/model/test4.php:42
107
- Stack trace:
75
+ array(0) {
108
- #0 /var/www/html/waiis/model/test4.php(67): BASE_INFO->Hostid_Check('2022-07-05 00:0...', '2022-09-27 12:4...')
109
- #1 /var/www/html/waiis/model/test4.php(72): Func_Exec()
110
- #2 {main}
76
+ }
111
- thrown in /var/www/html/waiis/model/test4.php on line 42
112
77
  ```
113
78
 
114
79
  ### 参考URL