前提・実現したいこと
ブラウザ上にサンプルのPHPを表示させたい
AWSのカリキュラムを学習しているのですが2周してもわからなかったので、どうかわかる方教えていただければものすごく助かります!
ここに質問の内容を詳しく書いてください。
AWSで作成したdbサーバーのMysqlの内容をwebサーバーのグローバルアドレスからブラウザ上に表示させたい
発生している問題・エラーメッセージ
ブラウザ上に下記のエラーメッセージが表示されて、該当サンプルのPHPファイルの内容が表示されない。
エラーメッセージ
Fatal error: Uncaught TypeError: PDO::__construct() expects parameter 4 to be array, string given in /var/www/html/index.php:12 Stack trace: #0 /var/www/html/index.php(12): PDO->__construct('mysql:host=10.0...', 'root', 'root', 'rootroot') #1 {main} thrown in /var/www/html/index.php on line 12
### 該当のソースコード (ターミナル上) Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.7.24 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database simple_blog; Query OK, 1 row affected (0.00 sec) mysql> use simple_blog;create table posts (id int not null primary key, title varchar(100), detail varchar(1000), image varchar(1000)); Database changed Query OK, 0 rows affected (0.02 sec) mysql> mysql> insert into posts values (1, "XXXX", "XXXXXXXXXX", "./img/img1.jpeg"); Query OK, 1 row affected (0.00 sec) mysql> mysql> insert into posts values (2, "YYYY", "YYYYYYYYYY", "./img/img2.jpeg"); Query OK, 1 row affected (0.01 sec) mysql> grant all privileges on *.* to root@"%" identified by 'rootroot' with grant option; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> select * from posts; +----+-------+------------+-----------------+ | id | title | detail | image | +----+-------+------------+-----------------+ | 1 | XXXX | XXXXXXXXXX | ./img/img1.jpeg | | 2 | YYYY | YYYYYYYYYY | ./img/img2.jpeg | +----+-------+------------+-----------------+ 2 rows in set (0.00 sec) mysql> exit Bye [ec2-user@udemy-aws-14days-db-1a ~]$ exit ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー (PHPのサンプルファイル) <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>Hello World</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous"> </head> <body> <?php try { # xxx.xxx.xxx.xxx はプライベート IP アドレス $dbh = new PDO('mysql:host=10.0.21.24;dbname=simple_blog', "root", "root" ,"rootroot"); $sth = $dbh->prepare("SELECT * from posts"); $sth->execute(); $posts = $sth->fetchAll(); $dbh = null; } catch (PDOException $e) { print "エラー!: " . $e->getMessage() . "<br/>"; die(); } ?> <h2 class="text-center mt-3 mb-5">Simple Blog</h2> <div class="container"> <?php foreach ($posts as $post) : ?> <div class="mb-5 row"> <div class="col-sm-2 mb-2"> <img src=<?php echo $post["image"]; ?> class="w-75 rounded-circle"></img> </div> <div class="col-sm-10"> <h3><?php echo $post["title"]; ?></h3> <p> <?php echo $post["detail"]; ?> </p> </div> </div> <?php endforeach; ?> </div> </body> </html> ```ここに言語名を入力 ソースコード
試したこと
・ターミナル上で sudo service httpd restartのコマンドで再起動
・vimファイルの内容が誤っていないか確認
プライベートアドレス、パスワードが一致しているかどうか(ここではrootrootにしている)
・Fatal errorの意味を調べ、dbサーバーにうまくアクセスできていないとわかった(だが原因の解明までには至りませんでした)
初投稿でわかりづらいかと思いますが、ここではないか?とわかる方回答お待ちしております。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/02 08:33