如何设置新的AUTO_INCREMENT值?


7

我的模块代码中包含以下语句:

db_query("ALTER TABLE {tripletex_invoice} AUTO_INCREMENT = :number", array(':number' => $start_value));

SimpleTest期间,我收到以下错误消息:

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 
You have an error in your SQL syntax; check the manual that corresponds 
to your MySQL server version for the right syntax to use near ''20000'' at
line 1: ALTER TABLE {tripletex_invoice} AUTO_INCREMENT = :value; Array ( 
[:value] => 20000 ) in _tripletex_add_invoice_log() (line 1769 of 
/var/www/d7/sites/all/modules/tripletex/tripletex.module).

为什么?

Answers:


11

参数替换仅适用于WHERE子句,不适用于您尝试运行的查询。

您需要$start_value直接插入查询字符串中。如果该值来自不可靠的来源,那么显然您需要对其进行清理并确保其自身在正确的范围内:

$start_value = (int)$start_value; // Make sure it's an integer

// Do any other checks you need to here (bounds checking, etc.)

// Build the query string and run it
$sql = "ALTER TABLE {tripletex_invoice} AUTO_INCREMENT = $start_value";
db_query($sql);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.