PHP PDO code to insert in MySQL db does not work

The error message seems to indicate that you've connected to the DB fine, but that the project database hasn't been selected.

To be sure it's trying to correct with the right DSN, I'd try changing the connection string to contain values directly, rather than variables, i.e.:

'mysql:host=localhost;dbname=project'

This shouldn't make a difference, but it's worth checking.

If that doesn't work, and since you appear to be able to connect to MySQL, a workaround could be to include the database name as part of the query. So your query above would become:

$query=$dbh->prepare("INSERT INTO project.users (userName, userEmail) VALUES (?,?)");

Tags:

Mysql

Php

Pdo