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 8075521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:00:44+00:00 2026-06-05T15:00:44+00:00

Here I am sharing code of two functions. Both return an instance of a

  • 0

Here I am sharing code of two functions. Both return an instance of a PDO object. But one of them works fine when a statement is inserted using the PDO connection where as other one does not. The same identical code is used in both cases to insert a record.

This is the function whose returned PDO object works with the insert statement.

public function getPDO()    {

    /** mysql hostname */
$hostName = 'localhost';

/** mysql database name */
$dbName = 'testDB';

/** db user name */
$userName = 'root';

/** db password */
$password = '';

try {
    $str = "mysql:dbname=$dbName;host=$hostName";
    $db = new PDO($str, $userName, $password );
        return $db;
    }catch(PDOException $e) {
        echo "PDO connection failed. <br />";
        echo $e->getMessage();
        return null;
    }
 }

This is the function whose returned PDO object DOES NOT work with the insert statement. In this function the connection string and other connection parameters are read from static values defined in the class.

public function getPDO()    {
    try {
        $connStr = "mysql:dbName=".self::$dbName.";host=".self::$hostName;
    $db = new PDO($connStr, self::$userName, self::$password);
        return $db;
    }catch(PDOException $e) {
        echo "PDO connection failed. <br />";
        echo $e->getMessage();
        return null;
    }
 }

Here is how the static variables are defined:

    /** mysql hostname */
static $hostName = "localhost";

/** mysql database name */
static $dbName = "testDB";

/** db user name */
static $userName = 'root';

/** db password */
static $password = '';

And here is the piece of code which uses the PDO object to insert a record.

 public function save() {
     //get the PDO
 $db_func = new db_functions();
 $pdo = $db_func->getPDO();
 $query = "INSERT INTO city(name, state, country) VALUES ('Vienna', 'Virginia', 'USA')";
 echo $query;
 $count = $pdo->exec($query);
 if($count > 0) {
     echo '</br> Successful </br>';
 }else  {
     echo '</br> Unsuccessful </br>';
 }

 }

When I use the second function, the PDO object is created successfully but the insert fails. I do not see any error but the count is 0 and no records in the DB.

I am suspecting this has to do with the way I am declaring and using static variables in my code. Thanks for your help.

  • 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-06-05T15:00:46+00:00Added an answer on June 5, 2026 at 3:00 pm

    It’s not related to static variables, your second method just doesn’t pass the database name properly. It must be ‘dbname’ instead of ‘dbName’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for an svn to host code but I'm not interested in sharing
Here are a few example of classes and properties sharing the same identifier: public
Here the total height of all <div> 's are 900 pixels, but the jQuery
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
Here's the code in AlertTableView: - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSInteger index = 12345; NSLog(@AlertTableView:
How to extend the sharing feature with checkbox for default app? here is my
I have two pieces of code that I'm using to learn about multiprocessing in
I have two arrays and I'm trying to intersect them so I can run
I have some JavaScript that is sharing a request between two separate servers on
Here are two programs that naively calculate the number of prime numbers <= n.

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.