You hinted at it in your comment, but none of the answers here state it explicitly, so I will:
In MySQL, you cannot use an IF statement outside of the body of a stored procedure. The documentation implies this when it introduces the concept as "the IF statement for stored programs" (my emphasis).
As far as I can see, there is no easy way to control execution flow in statements issued at the command line prompt or via the client (e.g. from PHP), except by querying values, then switching control flow based on those values outside of MySQL (i.e. using PHP's if
statement instead of MySQL's). I would love to be corrected on this though :-)
You can simulate this on a case-by-case basis using other constructions.
For example:
IF x THEN
INSERT INTO mytable (mycol)
VALUES ('foo')
END IF
could become
INSERT INTO mytable (mycol)
SELECT 'foo'
FROM dual
WHERE x
(as per this answer)
You can also create, call and then drop a new stored procedure:
DELIMITER \\
CREATE PROCEDURE tmpfunc() BEGIN
IF x THEN
INSERT INTO mytable (mycol)
VALUES ('foo');
END IF;
END \\
DELIMITER ;
CALL tmpfunc();
DROP PROCEDURE tmpfunc;
(as per this answer)