I have the following PHP Code
$con = mysqli_connect($host, $user, $password, $database) or die ("Couldn't connect to database"); //assume these variables are declared
$sql = "INSERT INTO Users
(
FirstName
,MiddleName
,LastName
)
Values
(
'John'
,'A'
,'Smith'
);
SET @petID = LAST_INSERT_ID();
INSERT INTO Pets
(
UserID
,Name
)
Values
(
@petID
,'myPet'
);";
if(!mysqli_query($con, $sql))
{
die('Error: ' . mysqli($con));
}
mysqli_close($con);
When I attempt to execute this code, this error happens:
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 'SET @petID = LAST_INSERT_ID(); INSERT INTO Pets ( UserID ,Name ' at line 24
To test if there was an error with mySql syntax, I put the string in a file and ran it directly using: mysql -u root -p myDatabase < InsertSqlStatement.sql The statement executed without any problems.
What is wrong with my PHP Code?