sql文にPHP変数を代入しカラム名を配列に応じて変更したいのですが、文法エラーが発生してしまいます。
クォーテーションの違いを理解したはずでしたが...
ご教示いただけますよう、よろしくお願いいたします。
php
1function update_active_days($dbh, $active_day_str){ 2 $sql = 'SELECT column_name from information_schema.columns where table_name = "schedule"'; 3 $stmt = $dbh->prepare($sql); 4 $stmt->execute(); 5 $column_name = array_map('current', $stmt->fetchAll()); 6 //カラム名”id”を配列から削除 7 while(($index = array_search("id", $column_name, true )) !== false ) { 8 unset($column_name[$index]) ; 9 } 10 //カラム名の差分を取ってキーを0から始まる連番に振り直す 11 $column_name_diff_old = array_values(array_diff($column_name, $active_day_str)); 12 $column_name_diff_new = array_values(array_diff($active_day_str, $column_name)); 13 $cnt = count($column_name_diff_old); 14 if ($cnt == 0){ 15 exit; 16 }elseif($cnt > 0){ 17 for ($i=0; $i < $cnt; $i++){ 18 $sql = "SELECT '$column_name_diff_old[$i]' from schedule AS '$column_name_diff_new[$i]'"; 19 $stmt = $dbh->prepare($sql); 20 $stmt->execute(); 21 } 22 } 23}
エラー
Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''2018-09-01'' at line 1 in
回答2件
あなたの回答
tips
プレビュー