开发者

Truncate table in foreach breaking with table name 'order' [duplicate]

开发者 https://www.devze.com 2023-03-17 21:24 出处:网络
This question already has an answer here: Syntax error due to using a reserved word as a table or column name in MySQL
This question already has an answer here: Syntax error due to using a reserved word as a table or column name in MySQL (1 answer) Closed 8 years ago.

I am trying to work with a script written by a previous developer that empties a number of different tables. The problem开发者_运维问答 now is that I have added a table called 'order' and breaks the loop.

set_time_limit (0); 
$conn = mysql_connect('localhost', 'root', '') or die ('Error connecting to mysql');
mysql_select_db('database-name');

$tables = array(
    'address',
    'manufacturer',
    'order',
    'voucher_history',
    'voucher_theme',
);

foreach ($tables as $table) {
    $sql = sprintf('TRUNCATE TABLE %s', $table);
    printf('%s %s ', $sql, str_repeat('.', 73 - strlen($sql)));
    $result = mysql_query($sql);
    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }else {
        echo "Done!<br />";
    }
}


order is a reserved keyword so it be must enclosed in back quotes:

'`order`',


backquote your table names. Order is a reserved keyword and will cause your query to break

0

精彩评论

暂无评论...
验证码 换一张
取 消