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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:24:46+00:00 2026-06-04T07:24:46+00:00

I am working on a very small PHP and MySQL application. I have a

  • 0

I am working on a very small PHP and MySQL application. I have a class called User for manipulating user data in the database which contains a createUser method as shown below:-

/**
* Creates a new user record in the users table for a new user
*   
* @return void
*/
public function createUser($user_id, $user_name, $location_id) {

    // query to execute
    $query = "INSERT INTO 
                      users(user_id,user_name,location_id)
                      VALUES(:id,:name,:location)";

    // query parameters
    $parameters = array (
        ':id' => $user_id,
        ':name' => $user_name,
        ':location' => $location_id
    );

    $databaseInteractions = new DatabaseInteractions();
    $databaseInteractions->executeUpdate($this->_connection, $query, $parameters);

}

I have another class that will contain common methods for interacting with the database called DatabaseInteractions as shown in the above code. This class contains a method called executeUpdate for executing DML queries as shown below:-

/**
 * A function for executing DML queries using a prepared statement
 * @param $connection The database connection object
 * @param $query The dml query to be executed
 * @param $parameters The input parameters for the query as a hash 
 */
public function executeUpdate($connection,$query, $parameters) {

    if ($stmt = $connection->prepare($query)) {

        //bind query parameters
        foreach ($parameters as $key => $value) {
            $stmt->bindParam($key, $value);
        }
        //begin transaction
        $connection->beginTransaction();

        $stmt->execute();
        $stmt->closeCursor();

        //commit transaction
        $connection->commit();
    }
}    

When i call the createUser method in the User class as shown below,

$user->createUser(3,"NewUser",1);

The values inserted in the database are as follows:-

   user_id   user_name  location_id
    1              1             1

I spent some time debugging the code but still can’t seem to find the reason why this is happening. user_id is the PK and is an int. user_name is varchar and location_id is FK and is int.

I am a Java developer and new to PHP so any inputs related to php naming conventions, coding standards, etc are always welcome.

  • 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-04T07:24:47+00:00Added an answer on June 4, 2026 at 7:24 am

    Answer: reference semantics gotcha.

    The second parameter of bindParam is passed by reference:

    Binds a PHP variable to a corresponding named or question mark
    placeholder in the SQL statement that was use to prepare the
    statement. Unlike PDOStatement::bindValue(), the variable is bound as
    a reference and will only be evaluated at the time that
    PDOStatement::execute() is called.

    Therefore, when the statement is executed all three parameters will evaluate to whatever the last value of $value was — in this case, 1.

    To solve the problem, use the optional parameter of execute instead of explicitly binding the parameters:

    public function executeUpdate($connection,$query, $parameters) {
        if ($stmt = $connection->prepare($query)) {
            $connection->beginTransaction();
    
            $stmt->execute($parameters);
            $stmt->closeCursor();
    
            //commit transaction
            $connection->commit();
        }
    }    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a simple location aware iPhone application which is functionally working very
I used to have a small chat app(which was almost working), that uses PHP,
I'm building a very small web ERP application with PHP / mySQL / CodeIgniter
I am very experienced in working with open-source technologies like PHP, MySQL, Apache and
I have written an application which is working fine in Emulator . But when
I'm working on a small, very application-specific CMS. There's nothing financial going on, and
I'm working on a very small form that accepts credit card numbers, which will
I have been working through a very small-scale WPF project in order to familiarize
I'm working on a very small site, with almost zero budget as a favor
I'm working on a small MVC framework (it's really very small) in Scala. I'd

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.