Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6010033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:03:30+00:00 2026-05-23T02:03:30+00:00

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

  • 0

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','orange@gmail.com')

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, '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 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:

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:03:31+00:00Added an answer on May 23, 2026 at 2:03 am

    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 (?,?)");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having difficulties to display data from mysql database to PHP generated table. Interesting
I am having difficulties when trying to insert files into a SQL Server database.
I am having difficulties with forms, specifically ModelMultipleChoiceField. I've pieced together this code from
I have having difficulties on this one page login.php. Because of the nature of
I am having difficulties getting a span tag to work properly inside a table.
I'm having difficulties scraping dynamically generated table in ASPX. Trying to scrape the gas
I am having difficulties with a complicated (for me any way) query. The table
I am having difficulties with listing this type of data. Scenario is as follows:
im having difficulties figuring this out, ive looked at examples here and on the
I'm having difficulties believing this question hasn't been asked before, but I couldn't find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.