php - PHP 中存储过程的问题

标签 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

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

最佳答案

啊,我明白我做了什么,我似乎添加了一个不必要的额外查询,该行:

$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/

相关文章:

php - 使用 php 将图像插入 mysql 不起作用

php - Valum 的文件 uploader ?

mysql - 可怕的 MySQL 导入编码问题 - 重新审视

Python存储过程未运行,没有错误

mysql - 如何使用mysql从存储过程中删除重复项

PHP 代码嗅探器规则

php - 是否可以在我自己的域下托管 PHP Web 应用程序,但将 url 重写为另一个域名?

mysql - 连接来自两个多对多 SQL 结构的数据

php - MySQL - 如何使用文件名作为查询的一部分?

java - 调用 CallableStatement.execute() 时出现问题;