Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am getting a PDO error when trying to push my PHP application that uses a database to Heroku. Everything with the app works fine

I am getting a PDO error when trying to push my PHP application that uses a database to Heroku. Everything with the app works fine on cloud9 (online ide), but when it is pushed then an error is thrown. Below you will see the error logs from Heroku. Any ideas of how I can troubleshoot this?

2018-05-28T18:29:08.526557+00:00 app[web.1]: thrown in /app/database.php on line 17
2018-05-28T18:29:08.526876+00:00 app[web.1]: 10.79.229.184 - - [28/May/2018:18:29:08 +0000] "GET / HTTP/1.1" 500 - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
2018-05-28T18:29:08.923554+00:00 app[web.1]: [28-May-2018 18:29:08 UTC] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /app/database.php:17
2018-05-28T18:29:08.923715+00:00 app[web.1]: #0 /app/database.php(17): PDO->__construct('mysql:host=;dbn...', NULL, NULL)
2018-05-28T18:29:08.923574+00:00 app[web.1]: Stack trace:
2018-05-28T18:29:08.923825+00:00 app[web.1]: #1 /app/index.php(31): getDatabaseConnection()
2018-05-28T18:29:08.923834+00:00 app[web.1]: #2 {main}
2018-05-28T18:29:08.923902+00:00 app[web.1]: thrown in /app/database.php on line 17
2018-05-28T18:29:08.924281+00:00 app[web.1]: 10.79.229.184 - - [28/May/2018:18:29:08 +0000] "GET / HTTP/1.1" 500 - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
2018-05-28T18:29:08.922514+00:00 heroku[router]: at=info method=GET path="/" host=protected-scrubland-18991.herokuapp.com request_id=6a972e81-f5e0-4011-8a3b-9012e07a6617 fwd="198.189.249.57" dyno=web.1 connect=0ms service=2ms status=500 bytes=306 protocol=https

The code the error is referring to is the script below AND refers to line 17:

Line 17: $dbConn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);

Rest of the code:

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $dbConn; }

function insertItemsIntoDB($items) { if (!$items){ return; } $db = getDatabaseConnection(); foreach ($items as $item) { $itemName = $item['name']; $itemPrice = $item['salePrice']; $itemImage = $item['thumbnailImage']; $sql = "INSERT INTO item (item_id, name, price, image_url) VALUES (NULL, :itemName, :itemPrice, :itemURL)"; $statement = $db->prepare($sql); $statement->execute(array( itemName => $itemName, itemPrice => $itemPrice, itemURL => $itemImage)); } } function getMatchingItems($query, $category, $priceFrom, $priceTo, $ordering, $showImages) { $db = getDatabaseConnection(); $imgSQL = $showImages ? ', item.image_url' : ''; $sql = "SELECT DISTINCT item.item_id, item.name, item.price $imgSQL FROM item INNER JOIN item_category ON item.item_id = item_category.item_id INNER JOIN category ON item_category.category_id =category.category_id WHERE 1"; if(!empty($query)) { $sql .= " AND item.name LIKE '%$query%'"; } if (!empty($category)) { $sql .= " AND category.category_name = '$category'"; } if (!empty($priceFrom)) { $sql .= " AND item.price >= '$priceFrom'"; }

if (!empty($priceTo)) { $sql .= " AND item.price <= '$priceTo'"; } if (!empty($ordering)) { if ($ordering == 'product') { $columnName = 'item.name'; } else { $columnName = 'item.price'; } $sql.= " ORDER BY $columnName"; } $statement = $db->prepare($sql); $statement->execute(); $items = $statement->fetchAll(); return $items; } function getCategoriesHTML() { $db = getDatabaseConnection(); $categoriesHTML = ""; // User can opt to not select a category $sql = "SELECT category_name FROM category"; $statement = $db->prepare($sql); $statement->execute(); $records = $statement->fetchAll(PDO::FETCH_ASSOC); foreach ($records as $record) { $category = $record['category_name']; $categoriesHTML .= ""; } return $categoriesHTML; } function addCategoriesForItems($itemStart, $itemEnd, $category_id) { $db = getDatabaseConnection(); for ($i = $itemStart; $i <= $itemEnd; $i++) { $sql = "INSERT INTO item_category (grouping_id, item_id, category_id) VALUES (NULL, '$i', '$category_id')"; $db->exec($sql); } } ?>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

13th Edition Global Edition

1292263350, 978-1292263359

More Books

Students also viewed these Databases questions

Question

Does your product/program have a descriptive and memorable slogan?

Answered: 1 week ago