Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Using PHP with MySQL Querying the Database with PHP Retrieving the Result Set

Tom Sager
Tom Sager
18,987 Points

Specifying the database

I cannot get $db->query( "SELECT * FROM products") to work - I have to use "SELECT * FROM shirts4mike.products". Why is the default database name not coming from the PDO object?

My code looks like this:

$db = new PDO("mysql:host=localhost;db=shirts4mike;port=80","root","foo");
$r1 = $db->query("show tables in shirts4mike");  // this works
$r2 = $db->query("select * from shirts4mike.products");  // this works
$r3 = $db->query("select * from products");  // this throws an error message:
```php

string(64) "SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected"

7 Answers

Tom Sager
Tom Sager
18,987 Points

So this keeps happening. To get rid of the error messages, I added the following at the bottom of the try block in database.php:

$db->exec("USE ".DB_NAME);

Tom Sager
Tom Sager
18,987 Points

Reformatting line breaks without markdown:

$db = new PDO("mysql:host=localhost;db=shirts4mike;port=80","root","foo"); $r1 = $db->query("show tables in shirts4mike"); // this works $r2 = $db->query("select * from shirts4mike.products"); // this works $r3 = $db->query("select * from products"); // this throws an error message

string(64) "SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected"

Michael Collins
Michael Collins
433 Points

Just at a quick glance, I'd say you'll need to at least correct the port number. Your web server runs on port 80. MySQL, by default, runs on port 3306.

Are you using MAMP? If so, it runs on port=8889 by default. The second parameter name is also incorrect. It should be dbname. If you are only making queries against the products table, the value should be set to the table. So your PDO object should look like this:

PDO("mysql:host=localhost;dbname=products;port=8889","root","foo")

Hope this helps.

Tom Sager
Tom Sager
18,987 Points

I am running LAMP, and port 80 is mapped in ports.conf. The first query would not work if this were a port issue.

he name of the database is shirts4mike and the table is products. The second query would not work otherwise.

However, after going away for the weekend and rebooting everything this morning, things are working as expected. So please mark this thread as solved. Thank you to Michale and Jordan for the responses.

Michael Collins
Michael Collins
433 Points

Just an FYI. ports.conf is an Apache web server configuration file. So, that should be mapping to port 80, unless you are using SSL, in which case it would be mapping to port 443.

There's a file, my.cnf which is your MySQL configuration file.

Rodger Voelkel
Rodger Voelkel
21,736 Points

Have you tried this?

$db = new PDO("mysql:host=localhost;dbname=shirts4mike;port=80","root","foo");

I believe in PDO the correct syntax is dbname not db. I could be wrong but the only other difference i see from my DB wrapper is that i dont call out a port explicitly. So would be more like...

$db = new PDO("mysql:host=localhost;dbname=shirts4mike;"root","foo");