Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the table from a PDOStatement object?

Tags:

database

php

pdo

Say I have a PDOStatement object generated via PDO->query($query), is it possible to get the table it was executed on?

Something like this:

<?php
$statement = $pdo->query('SELECT * FROM `foo`;');
echo $statement->xyz;
// foo

I'm fully aware you can use $query->getColumnMeta(0)['table'] to do it, but as mentioned by the docs, it's not very safe. This needs to work across all PDO drivers.

like image 589
Nate Higgins Avatar asked Oct 18 '12 21:10

Nate Higgins


1 Answers

You can retreive the name of the table using the PDOStatement that retrieves an associative array. The value ['name'] is the name of the table.

    $select = $conn_pdo->query('SELECT * FROM foo');
    $meta = $select->getColumnMeta(0);
    echo "Name of table: ".$meta['table'];
like image 162
Circum Avatar answered Nov 16 '22 04:11

Circum