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` ...