データベースを以下のように定義して、
SQL
1CREATE TABLE `test_database`(
2 `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
3 `username` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
4 `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
5 `role` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
6 `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
7 `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
8) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci
質問者さんのコードをこちらで動作するようにして動作させたところ
PHP
1<?php
2$dsn = "mysql:dbname=sample01;host=localhost;charset=utf8";
3$user = "username";
4$password = "password";
5$options = [
6 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
7 PDO::ATTR_EMULATE_PREPARES => false,
8];
9
10try {
11 $dbh = new PDO($dsn, $user, $password, $options);
12} catch (PDOException $exception) {
13 echo $exception->getMessage();
14 exit();
15}
16
17function create_hash($raw)
18{
19 if ($raw === '' || is_null($raw)) {
20 $hashed = password_hash(time() * rand(), PASSWORD_BCRYPT, array('cost' => 10));
21 } else {
22 $hashed = password_hash($raw, PASSWORD_BCRYPT, array('cost' => 10));
23 }
24 return $hashed;
25}
26
27function debugPrint($var)
28{
29 echo '<pre>';
30 print_r($var);
31 echo '</pre>';
32}
33
34$list = [
35 ['user1', 'user1', 'admin'],
36 ['user2', 'user2', 'staff'],
37 ['user3', 'user3', 'staff'],
38 ['user4', '', 'parttime'],
39 ['user5', '', 'parttime'],
40 ['user6', '', 'parttime'],
41];
42
43foreach ($list as $row) {
44 try {
45 $stmt = $dbh->prepare("insert into `test_database` (username, password, role, created_at, updated_at) values (?, ?, ?, now(), now())");
46 $stmt->execute([$row[0], create_hash($row[1]), $row[2]]);
47 } catch (PDOException $exception) {
48 echo $exception->getMessage();
49 exit();
50 }
51}
52
53$stmt = $dbh->query("select * from `test_database`");
54$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
55
56debugPrint($rows);
正常に動作しているように見えるのですが、一度SQLなどでエラーが発生していないか、データベースへ接続する箇所のコードは合っているかなどをもう一度確認していただけませんか?
以下、SELECTの結果です。
Array
(
[0] => Array
(
[id] => 1
[username] => user1
[password] => $2y$10$zLRvAtx8E3vDrXR01M7Bf..sCepi.ErzkOqfRJ76aXAnCr3HU2OXG
[role] => admin
[created_at] => 2017-05-08 00:11:55
[updated_at] => 2017-05-08 00:11:55
)
[1] => Array
(
[id] => 2
[username] => user2
[password] => $2y$10$eGRbwfGTllNhAP.IJ57PBuD5scZ3Kl.GufxiekaRxBw1lGGbousy6
[role] => staff
[created_at] => 2017-05-08 00:11:55
[updated_at] => 2017-05-08 00:11:55
)
[2] => Array
(
[id] => 3
[username] => user3
[password] => $2y$10$KEsVgUjEjDqx.PGj1Sred.q568JZ18zc0g6bQ2LcQWZS7uSRuleOq
[role] => staff
[created_at] => 2017-05-08 00:11:55
[updated_at] => 2017-05-08 00:11:55
)
[3] => Array
(
[id] => 4
[username] => user4
[password] => $2y$10$hJaHNdmS0ZvYifR9RvQ7/.NR6fbqJ/ex.byNiNITSGpMwoIA439Iq
[role] => parttime
[created_at] => 2017-05-08 00:11:55
[updated_at] => 2017-05-08 00:11:55
)
[4] => Array
(
[id] => 5
[username] => user5
[password] => $2y$10$5T6DJLXWkPWk6popEy5PneOPwfposSu2UbMrXE4yZy6cpDQjZulza
[role] => parttime
[created_at] => 2017-05-08 00:11:55
[updated_at] => 2017-05-08 00:11:55
)
[5] => Array
(
[id] => 6
[username] => user6
[password] => $2y$10$5y6ucEvixJGoxlO9H0TAROlGD6.D3Tk2ZaeXlZc/h8OWw5U27qu22
[role] => parttime
[created_at] => 2017-05-08 00:11:55
[updated_at] => 2017-05-08 00:11:55
)
)
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/05/07 15:38
2017/05/07 15:46 編集
退会済みユーザー
2017/05/07 15:51