我有以下存储过程,当我运行我的程序时,它可以正确执行:
$insertIntoEmployeesProcedure = "
CREATE PROCEDURE EmployeeInsert(name VARCHAR(50),password VARCHAR(50), email VARCHAR(50))
BEGIN
INSERT INTO employees(name,password,email) values(name,password,email);
END";
$returnInsertIntoEmpProc = $conn->query($insertIntoEmployeesProcedure);
if(! $returnInsertIntoEmpProc )
{
die('Could not create insert procedure: ' . $conn->error);
}
else
{
echo "Insert Procedure created successfully<br/>";
}
然后,当需要时,我在另一个类中调用此过程:
$insertEmp = mysqli_query($conn, "Call EmployeeInsert('$username','$password', '$email')");
$executeInsertEmp = $conn->query($insertEmp);
if(!$executeInsertEmp )
{
die('Employees not added: ' . $conn->error);
}
else
{
echo "Employees added<br/>";
}
问题是,当我执行此代码时,出现以下错误
Employees not added: 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 '1' at line 1
我遇到的主要问题是,即使它返回此错误,记录仍会添加到数据库中,并且一切似乎都工作正常。我想我更好奇为什么我会收到这个错误,因为我显然忽略了一些东西。
最佳答案
啊,我明白我做了什么,我似乎添加了一个不必要的额外查询,该行:
$executeInsertEmp = $conn->query($insertEmp);
可以省略,然后对保存存储过程的变量进行 if 语句中的检查。以下代码有效:
$insertEmp = mysqli_query($conn, "Call EmployeeInsert('$username','$password', '$email')");
if(!$insertEmp )
{
die('Employees not added: ' . $conn->error);
}
else
{
echo "Employees added<br/>";
}
关于php - PHP 中存储过程的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27195947/