我有以下 PHP 代码
$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);
当我尝试执行此代码时,会发生此错误:
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
为了测试 mySql 语法是否有错误,我将字符串放在一个文件中并直接运行它:mysql -u root -p myDatabase < InsertSqlStatement.sql
语句执行没有任何问题。
我的 PHP 代码有什么问题?