Skip to content

Non back-tick table names starting with integer in an UPDATE/DELETE statements can't be executed. #16057

Closed

Description

@MatthewAsia

Describe the bug

UDATING/DELETING/INSERTING with non back-ticked table names that start with an integer cause errors, though it is valid in MySQL. (Probably other query types too)

Works fine:

UPDATE `0searchhistoryx` SET complete = 1 WHERE id = 1;

Causes error:

UPDATE 0searchhistoryx SET complete = 1 WHERE id = 1;

To Reproduce

Steps to reproduce the behavior:

  1. Open database
  2. Create table named 0searchhistoryx
  3. Open SQL tab
  4. Enter SQL "UPDATE 0searchhistoryx SET complete = 1 WHERE id = 1;"
  5. Click "Go"
  6. See error
    =====
    Error in processing request
    Error code: 500
    Error text: Internal Server Error (rejected)
    It seems that the connection to server has been lost. Please check your network connectivity and server status.
    =====
> Fatal error</b>:  Uncaught TypeError: Argument 1 passed to PhpMyAdmin\DatabaseInterface::escapeString() must be of the type string, null given, called in /var/www/html/ask/pmaask/libraries/classes/Tracker.php on line 149 and defined in /var/www/html/ask/pmaask/libraries/classes/DatabaseInterface.php:3000
> Stack trace:
> #0 /var/www/html/ask/pmaask/libraries/classes/Tracker.php(149): PhpMyAdmin\DatabaseInterface->escapeString()
> #1 /var/www/html/ask/pmaask/libraries/classes/Tracker.php(841): PhpMyAdmin\Tracker::isTracked()
> #2 /var/www/html/ask/pmaask/libraries/classes/DatabaseInterface.php(354): PhpMyAdmin\Tracker::handleQuery()
> #3 /var/www/html/ask/pmaask/libraries/classes/Sql.php(958): PhpMyAdmin\DatabaseInterface->tryQuery()
> #4 /var/www/html/ask/pmaask/libraries/classes/Sql.php(1177): PhpMyAdmin\Sql->executeQueryAndMeasureTime()
> #5 /var/www/html/ask/pmaask/libraries/classes/Sql.php(2225): PhpMyAdmin\Sql->executeTheQuery()
> #6 /var/www/html/ask/pmaask/import.php(760): PhpMyAdmin\Sql->executeQueryAndGetQueryResponse()
> #7 {main}
>   t in <b>/var/www/html/ask/pmaask/libraries/classes/DatabaseInterface.php</b> on line <b>3000

=====

Expected behavior

Runs SQL happily

Screenshots

If applicable, add screenshots to help explain your problem.

Server configuration

  • Operating system: Centos 8
  • Web server: Apache/2.4.37 (centos)
  • Database version: MySQL 8.0.17
  • PHP version: 7.4.3 & 7.4.4
  • phpMyAdmin version: 5.0.1 & 5.0.2

Client configuration

  • Browser: Chrome
  • Operating system: Windows 10

Additional context

Add any other context about the problem here.

Class \PhpMyAdmin\SqlParser\Lexer separates the 0 from the rest of the table name.

    [0] => PhpMyAdmin\SqlParser\Token Object
        (
            [token] => UPDATE
            [value] => UPDATE
            [keyword] => UPDATE
            [type] => 1
            [flags] => 3
            [position] => 0
            [method] => parseKeyword
        )

    [1] => PhpMyAdmin\SqlParser\Token Object
        (
            [token] =>  
            [value] =>  
            [keyword] => 
            [type] => 3
            [flags] => 0
            [position] => 6
            [method] => parseWhitespace
        )

    [2] => PhpMyAdmin\SqlParser\Token Object
        (
            [token] => 0
            [value] => 0
            [keyword] => 
            [type] => 6
            [flags] => 0
            [position] => 7
            [method] => parseNumber
        )

    [3] => PhpMyAdmin\SqlParser\Token Object
        (
            [token] => searchhistoryx
            [value] => searchhistoryx
            [keyword] => 
            [type] => 0
            [flags] => 0
            [position] => 8
            [method] => parseUnknown
        )

    [4] => PhpMyAdmin\SqlParser\Token Object
        (
            [token] =>  
            [value] =>  
            [keyword] => 
            [type] => 3
            [flags] => 0
            [position] => 22
            [method] => parseWhitespace
        )

    [5] => PhpMyAdmin\SqlParser\Token Object
        (
            [token] => SET
            [value] => SET
            [keyword] => SET
            [type] => 1
            [flags] => 11
            [position] => 23
            [method] => parseKeyword
        )

    [6] => PhpMyAdmin\SqlParser\Token Object
        (
            [token] =>  
            [value] =>  
            [keyword] => 
            [type] => 3
            [flags] => 0
            [position] => 26
            [method] => parseWhitespace
        )

    [7] => PhpMyAdmin\SqlParser\Token Object
        (
            [token] => `complete`
            [value] => complete
            [keyword] => 
            [type] => 8
            [flags] => 2
            [position] => 27
            [method] => parseSymbol
        )

My quick fixes for simple queries: (Not the best way I'm sure)
Statement.php - Add at line 263

            // UPDATE 0table
            // Fixes table names starting with a integer in updates
            $lastIdx = $list->idx;
            if ($this instanceof Statements\UpdateStatement) {
                if ($token->value === 'UPDATE') {
                    ++$list->idx; // Skip ON
                    $first = $list->getNext();
                    $second = $list->getNext();
                    if ($first->type == Token::TYPE_NUMBER && $second->type == Token::TYPE_NONE) {
                        $first->type = Token::TYPE_WHITESPACE;
                        $first->value = ' ';
                        $second->type = Token::TYPE_SYMBOL;
                        $second->value = $first->value . $second->value;
                    }
                }
            }
            $list->idx = $lastIdx;

DeleteStatement.php - Add at line 242

        // DELETE 0table
        // Fixes table names starting with a integer in updates
        $lastIdx = $list->idx;
        for (; $list->idx < $list->count; ++$list->idx) {
            $token = $list->tokens[$list->idx];
            if ($token->value === 'FROM') {
                ++$list->idx; // Skip ON

                $first = $list->getNext();
                $second = $list->getNext();
                if ($first->type == Token::TYPE_NUMBER && $second->type == Token::TYPE_NONE) {
                    $first->type = Token::TYPE_WHITESPACE;
                    $first->value = ' ';
                    $second->type = Token::TYPE_SYMBOL;
                    $second->value = $first->value . $second->value;
                }
                break;
            }

        }
        $list->idx = $lastIdx;
        // END DELETE FIX

Perhaps should be fixed in Lexer instead

Activity

  1. added
    Bug
    on Mar 31, 2020
  2. added this to the 5.0.3 milestone on Mar 31, 2020
  3. williamdes commented on Mar 31, 2020

    @williamdes
    Member

    Thank you for the detailed report, I hope that it will be fixed soon :)
    If you want you can open a pull-request on the https://github.com/phpmyadmin/sql-parser repository to branch QA

  4. MatthewAsia commented on Apr 4, 2020

    @MatthewAsia
    Author

    Also appears to happen with INSERT INTO 0table

  5. Tithugues commented on Apr 14, 2020

    @Tithugues
    Contributor

    @MattyTheMad , thanks for noticing it. With the changes, it seems to fix the INSERT also.

  6. added
    waiting on upstream
    on Apr 14, 2020
  7. added a commit that references this issue on May 1, 2020
  8. shucon commented on Oct 8, 2020

    @shucon
    Contributor

    Is it done?

  9. williamdes commented on Oct 8, 2020

    @williamdes
    Member
  10. ibennetch commented on Oct 8, 2020

    @ibennetch
    Member
  11. modified the milestones: 5.0.3, 5.0.4 on Oct 10, 2020
  12. self-assigned this
    on Oct 10, 2020
  13. modified the milestones: 5.0.4, 5.0.3 on Oct 10, 2020
  14. locked as resolved and limited conversation to collaborators on Oct 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Bugparserwaiting on upstream

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions