Is there any safe way to parameterize database names in MySQL queries?

前端 未结 2 1939
北恋
北恋 2021-01-14 18:55

I\'m writing a little python script to help me automate the creation of mysql databases and associated accounts for my personal projects. Part of this script is a function t

2条回答
  •  隐瞒了意图╮
    2021-01-14 19:55

    After some digging it turns out that phpmyadmin uses backticks to quote database, table, and column names. They simply do:

    $sql_query = 'CREATE DATABASE ' . PMA_backquote($new_db);  
    

    Which would give in the error case above something like

    CREATE DATABASE `test_db; DROP some_other_db`;
    

    Of course any backticks in the input string need to be escaped, which according to phpmyadmin's code is done by replacing all single back ticks with double back ticks. I can't find any where that confirms that this is correct.

    I also noticed online though that backticks are not standard SQL.

提交回复
热议问题