[英]Issue with stored procedure in PHP
我有以下存儲過程在運行程序時可以正確執行:
$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/>";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.