2016-02-27 61 views
-1

我知道這可能是一個愚蠢的問題,解決方案很簡單,但我花了最後兩個小時試圖找出這個問題。我在PHP中收到有關我的SQL查詢的錯誤如下:嘗試將解析的XML值加載到MySQL時出錯

您的SQL語法錯誤;檢查手冊, 對應於您的MySQL服務器版本的正確的語法使用 附近'組)VALUES(772,1227,818,0,16,2,9,'21 -35',213)'在 線1

通過研究,我認爲這是我插入的數據類型的錯誤。我的表是這樣設置:

schedules table

通過使用獲取類型我發現我的值是字符串。我在解析XML文件時將它們轉換爲int,並使用get-type進行檢查,以確保一切正常。我評論了get-type命令旁邊的結果。這是我用來提取值並將它們加載到日程表中的PHP代碼。

function loadSchedules($xml, $connection) 
    { 
     echo "\n\r" . 'Loading Schedules'; 
     $connection->query("TRUNCATE TABLE schedules"); 
     foreach($xml->schedules->schedule as $item) 
     { 
      $schedule_id = intval($connection->real_escape_string($item->attributes()->id)); 
      $course_id = intval($connection->real_escape_string($item->attributes()->course_id)); 
      $turn_part_id = intval($connection->real_escape_string($item->attributes()->turn_part_id)); 
      $day = intval($connection->real_escape_string($item->attributes()->day)); 
      $units_in_day = intval($connection->real_escape_string($item->attributes()->units_in_day)); 
      $duration = intval($connection->real_escape_string($item->attributes()->duration)); 
      $room_id = intval($connection->real_escape_string($item->attributes()->room_id)); 
      $period = $connection->real_escape_string($item->attributes()->period); 

      //Saznaj za koju se grupu odnosi preko turn_part_id - lakše je tako nego da mobitel to radi, brže će se app izvoditi 

      $result = $connection->query("SELECT * FROM courses WHERE turn_part_id=$turn_part_id"); 
      while($row = mysqli_fetch_array($result)) 
      { 
       //print $row['groups']; 
       $group = $row['groups']; 
       //echo "\n\r" . gettype($group); // String 

       $group = intval($group); 


       echo "\n\r schedule_id " . gettype($schedule_id); // Integer 
       echo "\n\r course_id " . gettype($course_id); // Integer 
       echo "\n\r turn_part_id " . gettype($turn_part_id); // Integer 
       echo "\n\r day " . gettype($day); // Integer 
       echo "\n\r units_in_day " . gettype($units_in_day); // Integer 
       echo "\n\r duration " . gettype($duration); // Integer 
       echo "\n\r room_id " . gettype($room_id); // Integer 
       echo "\n\r period " . gettype($period); // String 
       echo "\n\r group " . gettype($group); // Integer 

       //die; 
       if(!mysqli_query($connection, "INSERT INTO `schedules` (schedule_id, course_id, turn_part_id, day, units_in_day, duration, room_id, period, group) VALUES ($schedule_id, $course_id, $turn_part_id, $day, $units_in_day, $duration, $room_id, '$period', $group);")) 
       { 
        echo mysqli_error($connection); 
       } 
      } 
     } 
    } 

我試圖從XML(因爲它們是不相關的我的項目,我跳過一些值加載一個例子線,這是一所學校時間表應用,我解析Wisetimetable生成的XML和加載將值存入MySQL數據庫,以便我可以從Android設備檢索這些數據)。

<schedule id="1" course_id="691" turn_part_id="1" day="0" units_in_day="18" duration="4" room_id="7" period="21-35" flags="1"/> 

有人能指點我做錯了什麼嗎?我會很感激我得到的任何答案,我仍然在學習,並且很久沒有使用過SQL。

+2

'group'是SQL中的保留字,您需要使用backtics將其轉義。 SQL語法錯誤始終是*語法*錯誤,而不是數據類型錯誤。 – Kenney

+0

我知道這是微不足道的。你可以把它作爲答案張貼,這樣我可以標記它嗎?謝謝,你剛剛救了我的一天:) –

+0

不客氣!順便說一句,這是很好,你[防止SQL注入](stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php),但你可以保存一些代碼行使用' ?佔位符 - 這種方式會自動處理。 – Kenney

回答

1

MySQL有麻煩解析查詢

INSERT INTO `schedules` (schedule_id, ...., group) 
VALUES (....) 

因爲GROUP是SQL的保留字。對於MySQL,你需要使用backtics來逃避它。

你看到這個任何時候:

您的SQL語法錯誤;檢查對應於你的MySQL服務器版本正確的語法近

這是因爲在SQL查詢本身語法錯誤的使用手冊。在對名稱和類型進行檢查之前解析查詢時會發生這種情況。