mardi 5 avril 2016

IF condition MySQL not working

I am trying to execute a query into MySQL but it keeps telling me i am using the wrong syntax, I tried searching the MySQL community but I am not getting anything usefull.. most of the answers i find on google are for other databases yet they label them for "MySQL", yet it keeps failing.

This is the statement i am trying to execute:

        $statement = "IF (SELECT ttb_id FROM timetable WHERE ttb_week = $i AND ttb_time = $j) THEN
                        BEGIN
                          UPDATE types SET typ_name = '$subj'
                          WHERE typ_name = 'student';
                        END;
                      ELSE
                        BEGIN
                          INSERT INTO types VALUES (null,`Yo`);
                        END;
                    ";

error:

ERROR: 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 'IF (SELECT ttb_id FROM timetable WHERE ttb_week = 0 AND ttb_time = 0) THEN ' at line 1

I am using:

  • PHP Script Language Version 5.2.6
  • MySQL Database Version 5.0.51b

I have been looking around but to no avail, and the if condition stated on the MySQL dev website are not helping at all..

I am new to this and it is driving me mad! all the different queries i tried failed.. it is never the right syntax.

I found many answers for the problem on stackExchange and other websites but it is always wrong.. also I remember this structure from a VB.net lesson a while ago so maybe this is for MSSQL? then what about MySQL? everyone so far listed similar structure and said it works for MySQL, I took my answer from an answer on this community* labeled for MySQL and he claimed it worked. this is one of many i tried.

I would really appreciate your help

*: Usage of MySQL's "IF EXISTS"

Aucun commentaire:

Enregistrer un commentaire