実行中のSQLステートメントで構文エラーが発生していますが、私の人生ではエラーが見つかりません。
プリペアドステートメントを作成して実行する機能を備えた独自のデータベースクラスを作成しました。これまで問題が発生したことがないため、実装に問題はないと思います。ただし、問題がある場合に備えて、そのコードも示します。
準備:
public function prepare($index, $sql) {
if(isset(self::$PS[$index])){
$ermsg = "Index [$index] is already in use.";
throw new Exception($ermsg, 1);
}
try{
self::$PS[$index] = $this->dbh->prepare($sql);
}
catch(PDOException $e){
return false;
}
return true;
}
そして実行します:
public function execute($index, Array $param = array()) {
if(!isset(self::$PS[$index])){
$ermsg = "Index [$index] is unavailable.";
throw new Exception($ermsg, 1);
}
foreach($param as $key => $val){
if(is_int($key)) ++$key;
$type = $this->getValueType($val);
$bnd = self::$PS[$index]->bindValue($key, $val, $type);
if(!$bnd){
$ermsg = "Paramater '$key' in [$index] failed to bind";
throw new Exception($ermsg, 2);
}
}
try{
$bnd = self::$PS[$index]->execute();
}
catch(PDOException $e){
$ermsg = "PDO-Error while executing prepared statement [$index] ".$e->getMessage();
throw new Exception($ermsg, 3);
}
if($bnd === false){
$ermsg = "Result error in prepared statement [$index]";
throw new Exception($ermsg, 3);
}
return self::$PS[$index];
}
私が言ったように、私はこれを使用して問題を経験したことがないので、それが問題だとは思いませんが、誰が知っていますか.
今私の実装:
$sql = "INSERT INTO ratings (course_id, overall, design, condition, service, value, rated_by, date_rated)
VALUES (:course_id, :overall, :design, :condition, :service, :value, :rated_by, now()))";
DBH::getInstance()->prepare('rateit', $sql);
$stmt = DBH::getInstance()->execute('rateit', array(
":course_id"=>$course_id,
":overall"=>$overall,
":design"=>$design,
":condition"=>$condition,
":service"=>$service,
":value"=>$value,
":rated_by"=>$log_user_id
));
}
if($stmt) {
echo 'suc, Thanks! Your ratings have been added to the course.';
exit();
}else{
echo 'err, There was an error rating the course. Please try again later';
exit();
}
これは、私が受け取った正確な構文エラー メッセージです。
構文エラーまたはアクセス違反: 1064 SQL 構文にエラーがあります。使用する正しい構文については、MySQL サーバーのバージョンに対応するマニュアルを確認してください'
誰かが私の構文エラーを見つけたり、これを引き起こしている何か他の問題を見つけたりすることができれば、それは素晴らしいことです. 助けてくれてありがとう。