php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74921 Wrong table name in PDO::getColumnMeta when using ATTR_EMULATE_PREPARES = false
Submitted: 2017-07-13 20:01 UTC Modified: -
From: asj dot ricardo at gmail dot com Assigned:
Status: Open Package: PDO MySQL
PHP Version: 7.1.7 OS: ANY
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: asj dot ricardo at gmail dot com
New email:
PHP Version: OS:

 

 [2017-07-13 20:01 UTC] asj dot ricardo at gmail dot com
Description:
------------
If we set ATTR_EMULATE_PREPARES = false, and create a alias to a view, the name set at getColumnMeta() to the table view is not the view alias, but the original view name

Test script:
---------------
<?php
$host = 'localhost';
$db = 'dbtest';
$user = 'user';
$pass = 'pass';
$pdo = new PDO('mysql:host='.$host.';dbname='.$db,$user,$pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$pdo->exec('DROP TABLE IF EXISTS foo');
$pdo->exec('DROP TABLE IF EXISTS bar');
$pdo->exec('DROP VIEW IF EXISTS bar_foo');

$pdo->exec('CREATE TABLE `bar` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `foo` int(11) unsigned NOT NULL,
  PRIMARY KEY (`id`)
)');

$pdo->exec('CREATE TABLE `foo` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `text` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
)');

$pdo->exec('CREATE VIEW `bar_foo`
AS SELECT
   `bar`.`id` AS `id`,
   `bar`.`foo` AS `foo`,
   `foo`.`id` AS `fooId`,
   `foo`.`text` AS `fooText`
FROM (`bar` join `foo` on((`bar`.`foo` = `foo`.`id`)))');

$values = Array('test1','test2','test3');
foreach($values as $v){
	$stmt = $pdo->prepare("INSERT INTO foo (`text`) VALUES (:y)");
	$stmt->bindParam(':y', $v);
	$stmt->execute();
}

$values = Array(1,2);
foreach($values as $v){
	$stmt = $pdo->prepare("INSERT INTO bar (foo) VALUES (:foo)");
	$stmt->bindParam(':foo', $v);
	$stmt->execute();
}

$result = $pdo->query('SELECT * FROM bar as bar2 join foo as foo2 on bar2.foo = foo2.id');
for($i=0;$i<$result->columnCount();$i++){
	$meta = $result->getColumnMeta($i);
	echo $meta['table'].':'.$meta['name']."\n";
}
echo "\n";
$result = $pdo->query('SELECT * FROM bar as bar2 join bar_foo as bar_foo2 on bar_foo2.id = bar2.id');
for($i=0;$i<$result->columnCount();$i++){
	$meta = $result->getColumnMeta($i);
	echo $meta['table'].':'.$meta['name']."\n";
}


Expected result:
----------------
bar2:id
bar2:foo
foo2:id
foo2:text

bar2:id
bar2:foo
bar_foo2:id
bar_foo2:foo
bar_foo2:fooId
bar_foo2:fooText

Actual result:
--------------
bar2:id
bar2:foo
foo2:id
foo2:text

bar2:id
bar2:foo
bar_foo:id
bar_foo:foo
bar_foo:fooId
bar_foo:fooText

Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Oct 19 12:01:27 2019 UTC