Truncate table in foreach breaking with table name 'order' [duplicate]
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
精彩评论