0

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 />";
    }
}
John Magnolia
  • 16,769
  • 36
  • 159
  • 270

2 Answers2

1

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

'`order`',
Clodoaldo Neto
  • 118,695
  • 26
  • 233
  • 260
0

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

Zak
  • 24,947
  • 11
  • 38
  • 68