PHP PDO code to insert into MySQL db fails

I'm having difficulty finding some simple PHP code to insert a record into a MySQL table.

This code, injected directly into WAMP, works great:

INSERT INTO `users` (`userName`,`userEmail`) VALUES ('orange','orange@gmail.com') 

This PHP code does not work:

 <?php $dbHost="localhost"; $dbName="project"; $dbUser="admin"; $dbPassword="abcd"; $dbh=new PDO("mysql:host=$dbHost;dbName=$dbName", $dbUser, $dbPassword); print_r($dbh); echo "</br>"; print_r($dbh->errorInfo()); $query=$dbh->prepare("INSERT INTO users (userName, userEmail) VALUES (?,?)"); echo "</br>"; print_r(var_dump($query->errorInfo())); echo "</br>"; print_r($query->errorCode()); echo "</br>"; print_r($dbh->errorInfo()); $query->bindValue(1, 'apple'); echo "</br>"; print_r(var_dump($query->errorInfo())); echo "</br>"; print_r($query->errorCode()); echo "</br>"; print_r($dbh->errorInfo()); $query->bindValue(2, 'apple@gmail.com'); echo "</br>"; print_r(var_dump($query->errorInfo())); echo "</br>"; print_r($query->errorCode()); echo "</br>"; print_r($dbh->errorInfo()); $inserted=$query->execute(); //True if succesful, False if not. echo "</br>"; print_r(var_dump($query->errorInfo())); echo "</br>"; print_r($query->errorCode()); echo "</br>"; print_r($dbh->errorInfo()); echo "</br>"; if ($inserted){print_r("true");}else{print_r("false");}; ?> 

What I get when the page is executed is the following listing:

 PDO Object ( ) Array ( [0] => [1] => [2] => ) array(3) { [0]=> string(0) "" [1]=> NULL [2]=> NULL } Array ( [0] => 00000 [1] => [2] => ) array(3) { [0]=> string(0) "" [1]=> NULL [2]=> NULL } Array ( [0] => 00000 [1] => [2] => ) array(3) { [0]=> string(0) "" [1]=> NULL [2]=> NULL } Array ( [0] => 00000 [1] => [2] => ) array(3) { [0]=> string(5) "3D000" [1]=> int(1046) [2]=> string(20) "No database selected" } 3D000 Array ( [0] => 00000 [1] => [2] => ) false 

The record is not added to db. What am I doing wrong? I'm not sure what I should see in print_r, I provide them with help for respondents.

Thanks,

Jdelage

edited . I added print_r recommended in the comments.

Here is what I see in WAMP:

http://jdelage.public.s3.amazonaws.com/project_sch.jpg

+8
php mysql pdo
source share
2 answers

The error message seems to indicate that you are well connected to the database, but the project database is not selected.

To make sure that it is trying to fix the correct DSN, I would try to change the connection string to contain values โ€‹โ€‹directly and not variables, for example:

 'mysql:host=localhost;dbname=project' 

This should not change, but worth checking.

If that doesn't work, and since you seem to be able to connect to MySQL, a workaround could be to include the database name as part of the query. So your query will look like this:

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

A very strange problem, it seems that you need to enter dbname lower case so that it connects to the database correctly.

So this should be:

 mysql:host=$dbHost;dbname=$dbName 
+1
source share

All Articles