Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO code to insert in MySQL db does not work [duplicate]

Tags:

php

mysql

pdo

I am having difficulties troubleshooting some simple PHP code to insert a record in a MySQL table.

This code entered directly into WAMP works fine:

INSERT INTO `users` (`userName`,`userEmail`) VALUES ('orange','[email protected]')

This PHP code doesn't 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, '[email protected]');
    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 I execute the page is the following printout:

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 isn't inserted in the db. What I am doing wrong? I am not sure what I should see in the print_r's, I am providing them as help for responders.

Thank you,

JDelage

edited - I added the print_r's recommended in the comments.

Here is what I see in WAMP:

like image 933
JDelage Avatar asked Jun 09 '11 20:06

JDelage


People also ask

How to connect a PHP script to MySQL using PDO?

The methods to connect a PHP script to MySQL using PDO is similar to the previous one, but with a slight variation: In the public_html, create a file named pdoconfig.php and insert the following code. As always, don’t forget to replace the placeholder values with your database information.

How to insert data into a MySQL database using PHP?

There are two methods you can use to INSERT data into your MySQL database. The PHP MySQLi method and PHP Data Object or PDO method. First, you’ll need to establish a connection to a database. After that is done, we can proceed with the MySQL query INSERT. Here is a full PHP code example with the basic connection and insert methods:

What is the difference between mysqli and PDO?

Meanwhile, MySQLi sees data as a set of interchangeable objects with functions, allowing users to add or remove data easily. PDO stands for PHP Data Object. Unlike MySQLi, PDO is only object-oriented and supports a number of different database types that use PHP, such as MySQL, MSSQL, Informix, and PostgreSQL.

How do I connect to a SQL database using PHP?

There are two methods to connect to an SQL database using PHP: MySQLi, and PDO. One of the most important features they both support is prepared statements, which accelerates the time needed for MySQL to execute the same query multiple times.


2 Answers

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 (?,?)");
like image 188
Dave Challis Avatar answered Oct 21 '22 21:10

Dave Challis


Very strange problem, it seems that you need to enter dbname in lowercase letters for it to correctly connect to the database.

So it should be:

mysql:host=$dbHost;dbname=$dbName
like image 28
jeroen Avatar answered Oct 21 '22 21:10

jeroen