I'm writing a user registration function in PHP PDO, and I have found that my query will only run fine if it is written like this:
<?php
$dbHost="localhost";
$dbName="project";
$dbUser="admin";
$dbPassword="abcd";
$dbh=new PDO("mysql:host=$dbHost;dbName=$dbName", $dbUser, $dbPassword);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$query=$dbh->prepare("INSERT INTO project.users (userName, userEmail) VALUES (?,?)");
.....
On the other hand, it will not run if I write:
...
$query=$dbh->prepare("INSERT INTO users (userName, userEmail) VALUES (?,?)");
...
In that case, I get the following error message:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected' in C:\wamp\www\Tests\Test03\Index.php:11 Stack trace: #0 C:\wamp\www\Tests\Test03\Index.php(11): PDOStatement->execute() #1 {main} thrown in C:\wamp\www\Tests\Test03\Index.php on line 11
Why is it that I need to precise project.users
? Why isn't it enough to enter the table name, given that the db name itself is already in the PDO object?
Thank you!
JDelage
UPDATE Please see accepted answer below. Replacing dbName=$dbName
with dbname=$dbName
solves this problem.