Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Databases

Michael Dowell
Michael Dowell
2,390 Points

Data could not be retrieved from the database.

In the course "Using PHP With MySQL":

I have tried 3 methods to get the database to work, with 3 different error messages.

1) Most recently, I tried downloading fresh source code from the downloads section of this link: https://teamtreehouse.com/library/using-php-with-mysql/using-relationship-tables-in-mysql/understanding-relationship-tables

When I run the code and visit any product page, such as: http://localhost:8888/shirts/108/

I am getting this error message: "Data could not be retrieved from the database."

2) Previously I coded alongside the tutorials starting from badge 1. When I got an error, I read other people's frustrations with the same issue and then tried all solutions that I have read so far, including copying the .htaccess folder which I have done successfully, and also using fresh source code from the 3rd previous badge. In those previous attempts, I got the following errors when I would visit any product page:

Server Error 500 404 Page Not found

Just wondering what I am missing here.

Thanks!

3 Answers

Michael Dowell
Michael Dowell
2,390 Points

I figured out a workaround. Here's what worked for me:

I commented out these lines in this file: inc/database.php: //$db = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME .";port=" . DB_PORT,DB_USER,DB_PASS); //$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); //$db->exec("SET NAMES 'utf8'");

And instead pasted in this line of code from an earlier version of the project: $db = new PDO("mysql:host=localhost;dbname=shirts4mike;port=8889","root","root");

Now the project works properly. I.e. individual shirt pages are displaying properly. For example: /shirts/102/

It's weird because I checked all the values of DB_HOST, DB_NAME, DB_PORT, DB_USER, DB_PASS and everything appears correct. Yet when I place in the hard-coded line everything works properly (i.e. individual t-shirt pages)

Still a bit baffled why this solution would work, and the original code doesn't.

Michael Timbs
Michael Timbs
15,761 Points

Check the config of XAMP / MAMP. I'd say you're running MySQL through the wrong port or MySQL servers have not been turned on. For me Skype and XAMP MySQL run through the same port so I can not have both running at once.

Michael Dowell
Michael Dowell
2,390 Points

Hi Michael,

Thanks, I appreciate your suggestion. Per your suggestion, I double-checked to make sure that MAMP is running through port 8888 for Apache and 8889 for MySQL. Those are the default settings. I also verified the code in inc > config.php to make sure the database is set to those same ports. Also I verified that MySQL is running via the MAMP launch window which shows a green mark in both the Apache server and MySQL checkboxes.

Then I checked MAMP > phpMyADMIN to make sure the database is named shirts4mike, which matches the name of the database in inc > config.php.

Everything looks correct to me. But I'm still getting the error: "Data could not be retrieved from the database."

Still looking for the solution to this...