1

重複の可能性:
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 サーバーのバージョンに対応するマニュアルで、near ''en の近くで使用する正しい構文を確認してください。 ') VALUES ('Test_tittle', 'Introduction', ' \r\n これは 1 行目の紹介 t' です )

おそらく\r\nを除いて、これらのいくつかのmysql行は正しいように見えるため、このエラーは私にはあまり意味がありません。しかし、私もやろうとしましたがhtmlentities($Introduction)、運が悪かったことはほとんどありません。ここの変数はすべて設定されており、null 以外の値が含まれています。

4

1 に答える 1

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());
于 2012-11-19T12:00:55.163 に答える