Bug #74921 [NEW]: Wrong table name in PDO::getColumnMeta when using ATTR_EMULATE_PREPARES = false
From: asj dot ricardo at gmail dot com
Operating system: ANY
PHP version: 7.1.7
Package: PDO MySQL
Bug Type: Bug
Bug description:Wrong table name in PDO::getColumnMeta when using ATTR_EMULATE_PREPARES = false
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
--
Edit bug report at https://bugs.php.net/bug.php?id=74921&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=74921&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=74921&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=74921&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=74921&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=74921&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=74921&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=74921&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=74921&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=74921&r=support
Expected behavior: https://bugs.php.net/fix.php?id=74921&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=74921&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=74921&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=74921&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=74921&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=74921&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=74921&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=74921&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=74921&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=74921&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=74921&r=mysqlcfg
Thread (2 messages)
- asj dot ricardo at gmail dot com