Q:

Error while trying to load parsed XML values into MySQL

I know this is probably a stupid question and the solution is trivial but I spent the last two hours trying to figure this one out. The error I receive in PHP regarding my SQL query is the following:

You have an error in your SQL syntax; check the manual that
  corresponds to your MySQL server version for the right syntax to use
  near 'group) VALUES (772, 1227, 818, 0, 16, 2, 9, '21-35', 213)' at
  line 1

By researching, I reckon it's an error with the data type I am inserting. My table is set like this:

By using get type I noticed that my values were strings. I converted them to an int while parsing the XML file and checked with get-type just to be sure. I commented the result next to the get-type command. This is the PHP code I am using to extract the values and load them in the schedules table.
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);
                }
            }
        }
    }

An example line I am trying to load from the XML (I'm skipping some values as they are not relevant to my project, it's a school schedule application and I'm parsing a Wisetimetable generated XML and loading the values into a MySQL DB so I can retrieve those data from an Android device).
<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"/>

Can someone point me what am I doing wrong? I'll appreciate any answer I get, I'm still learning and I haven't used SQL in a long time.

A:

MySQL has trouble parsing the query
INSERT INTO `schedules` (schedule_id, ...., group) 
VALUES (....)

because GROUP is a reserved word in SQL. For MySQL, you need to escape it using backtics.
Any time you see this:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 

it is because of a syntax error in the SQL query itself. This happens while parsing the query, before names and types are checked.

