Friday, 8 July 2016

php - Mysqli prepare giving error, even though SQL statement is valid?



I'm having some trouble with a SQL transaction using PHP and mySQL. Here is my statement:



START TRANSACTION;
INSERT INTO listings (title, price, seller, description, time, featured)
VALUES (?, ?, ?, ?, ?, 0);
SELECT @listingid:=LAST_INSERT_ID();

INSERT INTO pictures (listid, primarypic)
VALUES (@listingid, 1);
COMMIT;


While this statement works fine when I put it in directly using phpMyAdmin (substituting the ? marks), it seems to fail when I call



$stmt = $mysqli->prepare("statement");



Replace "statement" with the above statement. The fact that it fails before getting to the bind_param of the $stmt leaves me to believe that it is not a problem with the parameters, but rather the query.



Here is the exact error, if it helps:




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 'INSERT INTO listings (title, price, seller, description, time, featured) ' at line 2




Thanks in advance for your help!


Answer




I don't think mysqli can handle compound statements like that (especially since there is a separate commit() function). Instead of START TRANSACTION; at the beginning and COMMIT; at the end, you'd do $mysqli->autocommit(false); before the query and $mysqli->commit(); when you're done.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...