php / php/php-src

PDO_DBLIB: Fetching from statements sharing a conection results in wrong values being returned

Open
#11,552 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Extension: pdo_dblib Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

When using pdo_dblib, making 2 queries at the same time using the same conection results in corrupt statements
The keys are correct, but the results are from the last query
It's as if both statements are somehow sharing the same cursor under the hood

The following code:

<?php
$user = "REDACTED";
$pass = "REDACTED";
$host = "REDACTED";
$db   = "REDACTED";

$con = new \PDO("dblib:host=$host;dbname=$db;version=7.3;charset=UTF-8",$user,$pass,array(
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
));

$sta = $con->query("SELECT *
FROM (
    VALUES ('A0'),
           ('A1'),
           ('A2')
) AS MyTable(A)");

$stb = $con->query("SELECT *
FROM (
    VALUES ('B0'),
           ('B1'),
           ('B2')
) AS MyTable(B)");

var_export($sta->fetch()); echo "\n";
var_export($stb->fetch()); echo "\n";

var_export($sta->fetch()); echo "\n";
var_export($stb->fetch()); echo "\n";

Resulted in this output:

array (
  'A' => 'B0',
)
array (
  'B' => 'B1',
)
array (
  'A' => 'B2',
)
false

But I expected this output instead:

array (
  'A' => 'A0',
)
array (
  'B' => 'B0',
)
array (
  'A' => 'A1',
)
array (
  'B' => 'B1',
)
PHP Version

PHP 8.2.7

Operating System

Ubuntu 23.04

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the pdo_dblib statement handling exercised by the provided PHP reproduction, focusing on how fetch() behaves for two statements sharing one connection. Reproduce the interleaved queries and verify that each statement returns its own A or B rows in the expected order.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.