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

  • SEARCH
  • Home
  • 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 8965765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:52:53+00:00 2026-06-15T16:52:53+00:00

I have a very weird behaviour with PDO. I won’t go into much details

  • 0

I have a very weird behaviour with PDO. I won’t go into much details as it would take up way too much time but basically what I observed is that when I re-use a \PDOStatement that performs a simple INSERT I sistematically get a wrong value when invoking PDO::lastInsertId().

The first time I execute the statement it works fine and I get back the right id. Subsequent executions will instead always return ‘0’. This is even more weird because it happens only between tests (PHPUnit ones). So say I execute the insert using the prepared statement in test1 (working), in test2 it will fail miserably.

When executing multiple times the prepared statement in a non unit-testing environment (in a simple php file fro instance) it all works fine and the last inserted ids are always accurate. Very weird indeed.

Here’s the test (note that PersistencyManagerInstance is just a plain intsance of PersistencyManager):

<?php

class PersistencyManagerTest extends PHPUnit_Framework_TestCase {

   const DELETE_ALL = "TRUNCATE user";
   const ADD_USER = "INSERT INTO user values(null, :username, :password)";
   const CHECK_USER_EXISTENCE = "SELECT * FROM user WHERE username = :username AND password = :password";
   const DELETE_USER_BY_ID = "DELETE FROM user WHERE id = ?";

   protected $manager = null;

   public function __construct() {
      $this->manager = new PersistencyManagerInstance(PDOFactory::build());
   }

   public function setUp() {
      $this->manager->exec(self::DELETE_ALL);
   }

   public function tearDown() {
      $this->manager->exec(self::DELETE_ALL);
   }

   public function testInsert() {
      $user = new User("laurent", "password");
      $id = $this->manager->insert(self::ADD_USER, $user->export());
      $this->assertEquals("1", $id);
   }

   public function testInsertAgain() {
      $user1 = new User("laurent1", "password1");
      $id = $this->manager->insert(self::ADD_USER, $user1->export());
      $this->assertEquals("1", $id);
   }

   public function testQuery() {
      $user = new User("laurent", "password");
      $this->manager->insert(self::ADD_USER, $user->export());
      $results = $this->manager->query(self::CHECK_USER_EXISTENCE, $user->export());
      $this->assertEquals(1, count($results));
   }

   public function testExec() {
      $user = new User("laurent", "password---");
      $id = $this->manager->insert(self::ADD_USER, $user->export());
      $affected = $this->manager->exec(self::DELETE_USER_BY_ID, array($id));
      $this->assertEquals(1, $affected);
   }
}

testInsert works while testInsertAgain does not.

and here’s the class:

<?php

namespace memory\manager;

use \PDO;

abstract class PersistencyManager {

   /**
    * @var array An array of \PDOStatement objects
    */
   protected static $ps = array();

   /**
    * @var \PDO 
    */
   protected $connection = null;

   protected function prepareStmt($sql) {
      // return $this->connection->prepare($sql);
      $key = md5($sql);
      if (!isset(self::$ps[$key])) {
         self::$ps[$key] = $this->connection->prepare($sql);
      }
      return self::$ps[$key];
   }

   public function __construct(PDO $connection) {
      $this->connection = $connection;
      $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   }

   public function __destruct() {
      $this->connection = null;
   }

   /**
    * Good for SELECT operations. By default it fetches using arrays.
    * @param string $sql
    * @param array $values
    * @param integer $fetchStyle
    * @return array A list of matching elements (The elements' type depends on $fetchStyle)
    */
   public function query($sql, array $values = array(), $fetchStyle = PDO::FETCH_ASSOC) {
      $prepared = $this->prepareStmt($sql);
      $prepared->execute($values);
      $prepared->setFetchMode($fetchStyle);
      $all = $prepared->fetchAll();
      $prepared->closeCursor();
      return $all;
   }

   /**
    * Good for INSERT operations.
    * @param string $sql
    * @param array $values
    * @return string Last inserted element's id in string format
    */
   public function insert($sql, array $values = array()) {
      $prepared = $this->prepareStmt($sql);
      $prepared->execute($values);
      $prepared->closeCursor();
      return $this->connection->lastInsertId();
   }

   /**
    * Good for all the remaining routines.
    * @param string $sql
    * @param array $values
    * @return integer The number of effected rows
    */
   public function exec($sql, array $values = array()) {
      $prepared = $this->prepareStmt($sql);
      $prepared->execute($values);
      $count = $prepared->rowCount();
      $prepared->closeCursor();
      return $count;
   }
}

Any idea?

Cheers

  • 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-15T16:52:55+00:00Added an answer on June 15, 2026 at 4:52 pm

    guys I was starting a new connection at every test. That was the reason.

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

Sidebar

Related Questions

I have a very weird behaviour in a screen with a modeless dialog and
I am facing a very weird behaviour. I have the $answer_styles array with some
I have seen a very weird behaviour in Java's double variable, as I'm trying
I have a very weird thing to do and simply don't know how. This
I have a very weird problem.. I really do hope someone has an answer
I have a very weird problem in Firefox ( version 3.5.2), and I am
I have a very weird problem with PROLOG. I have used it before, but
Hello I have a very weird problem in chrome I just cannot figure out!
So, I have this very weird error - Some advanced PHP devs might also
I have encountered a very weird situation in Python. I have a while True:

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.