sqlite-database-integration icon indicating copy to clipboard operation
sqlite-database-integration copied to clipboard

MySQL proxy returns incorrect values for columns with duplicate names

Open elphizu opened this issue 3 weeks ago • 0 comments

The MySQL proxy returns incorrect values when a query has columns with the same name.

For example, SELECT 1 AS x, 2 AS x returns 1, 2 in MySQL, but the proxy returns 2, 2. This also affects joins with columns that share the same name.

The issue appears to be caused by PDO::FETCH_OBJ in SQLite_Adapter::handle_query(). It overwrites values with duplicate column names. MySQL_Protocol::build_row_packet() then reads the same value for both columns.

The MySQL on SQLite driver returns the correct values with PDO::FETCH_NUM, so the issue is in the proxy.

Step-by-step reproduction instructions

  1. Start the MySQL proxy:
    php packages/mysql-proxy/bin/wp-mysql-proxy.php --port 13307
    
  2. Connect to the proxy using a MySQL client.
  3. Run the following queries and compare the results.

Code snippet

$pdo = new PDO( 'mysql:host=127.0.0.1;port=13307', 'root', '' );

$q = $pdo->query( 'SELECT 1 AS x, 2 AS x' );
echo json_encode( $q->fetchAll( PDO::FETCH_NUM ) );
// Actual: [[2,2]]
// Expected: [[1,2]]

$q = $pdo->query( 'SELECT a.id, b.id FROM (SELECT 1 AS id) a JOIN (SELECT 2 AS id) b ON 1=1' );
echo json_encode( $q->fetchAll( PDO::FETCH_NUM ) );
// Actual: [[2,2]]
// Expected: [[1,2]]

Expected behavior

Each column should return its own value, even when column names are the same.

Actual behavior

The proxy returns the last column's value for all columns with the same name.

Environment info

  • Branch: trunk
  • PHP: 8.5

elphizu avatar Nov 12 '26 14:11 elphizu