[phpMyAdmin Developers] get the table name from sql query
Michal Čihař
michal at cihar.com
Wed Aug 2 13:51:23 CEST 2017
Hi
Manish Bisht píše v St 02. 08. 2017 v 16:48 +0530:
> I have one create sql query in one variable.
>
> Is there any way I can get the table name from the sql query using
> sql
> parser or any other other way.
Yes, SQL parser can do that:
<?php
require 'vendor/autoload.php';
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Utils\Query;
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
$query = 'CREATE TABLE t (c CHAR(20) CHARACTER SET utf8 COLLATE
utf8_bin)';
$parser = new Parser($query);
$statement = $parser->statements[0];
foreach ($parser->statements as $statement) {
if ($statement instanceof CreateStatement) {
echo "Creating table $statement->name\n";
}
}
>
> I am thinking of splitting the query by single space and then using
> the 3 value of the array as the table name.
No, please do not make any assumptions on the SQL query structure, this
is very easy to break:
CRATE TABLE `table with spaces` ...
CREATE TABLE IF NOT EXIST `table` ...
CREATE TABLE /* comment */ `table` ...
--
Michal Čihař | https://cihar.com/ | https://weblate.org/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <https://lists.phpmyadmin.net/pipermail/developers/attachments/20170802/1e5f33e7/attachment.sig>
More information about the Developers
mailing list