PHP中的存储过程问题

时间:2014-11-28 21:11:59

标签: php mysql stored-procedures

我运行程序时有以下存储过程正确执行:

$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

我遇到的主要问题是,即使它返回此错误,记录仍会添加到数据库中,一切似乎都正常。我想我更好奇为什么我收到这个错误显然我忽略了一些东西。

1 个答案:

答案 0 :(得分:0)

啊,我看到我做了什么,我似乎添加了一个额外的查询,这是不必要的,行:

$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/>";
}