可能重复:
Can you put placeholders in select part of a query using PDO?
我不想问这么“简单”的问题,但是这段代码不起作用,我无法弄清楚为什么在1小时后尝试($ this-> DB工作没有问题)。
此代码不起作用:
$STH = $this->DB->prepare("INSERT INTO chapters (subject_keyword, type, ?) VALUES (?, 'Introduction', ?)");
$STH->execute(array($this->Language, str_replace(' ', '_', $Title), $Introduction));
print_r($STH->ErrorInfo());
也不是扩展形式:
$t_lang = $this->Language;
$t_title = str_replace(' ', '_', $Title);
$t_intr = $Introduction;
$STH = $this->DB->prepare("INSERT INTO chapters (subject_keyword, type, ?) VALUES (?, 'Introduction', ?)");
$STH->execute(array($t_lang, $t_title, $t_intr));
print_r($STH->ErrorInfo());
返回的错误是:
DDC:164Array([0] => 42000 [1] => 1064 [2] =>您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以获得正确的语法在''en'附近使用)VALUES('Test_tittle','Introduction',' \ r \ n这是第1行的介绍t'
这个错误对我来说并不多,因为除了\ r \ n之外,这几条mysql线似乎是正确的。但我也尝试过做htmlentities($Introduction)
和其他一些没有运气的事情。这里的所有变量都是设置的,并且有一些非空值。
答案 0 :(得分:2)
尝试:
$t_lang = $this->Language;
if (!in_array($t_lang, array('en', 'jp', ... Your other languages ...))) {
throw new Exception(... Error message ...);
}
$t_title = str_replace(' ', '_', $Title);
$t_intr = $Introduction;
$STH = $this->DB->prepare("INSERT INTO chapters (subject_keyword, type, " . $t_lang . ") VALUES (?, 'Introduction', ?)");
$STH->execute(array($t_title, $t_intr));
print_r($STH->ErrorInfo());