PDO语句(SQL Server)的语法失败执行存储过程

问题描述 投票:1回答:1

我有以下错误:

PDOStatement :: execute():SQLSTATE [42000]:语法错误或访问冲突:102 [Microsoft] [SQL Server的ODBC驱动程序17] [SQL Server]'@IdIng'附近的语法不正确。在第64行的C:\ Apache24 \ localhost \ Class \ IngredientsManager.php中

我在IngredientsManager.php类中的相关方法是:

public function deleted(Ingredient $ingredient)
    {
        $db = parent::dbConnect();
        $stmt = $db->prepare("CALL DeleteIngredient( @IdIng=:code)");
        $code='['.$ingredient->code().']';
        $stmt->bindParam(':code', $code);
        $stmt->execute();
    }

我也尝试过:

public function deleted(Ingredient $ingredient)
        {
            $db = parent::dbConnect();
            $stmt = $db->prepare("CALL DeleteIngredient(?)");
            $code='['.$ingredient->code().']';
            $stmt->bindParam(1, $code, PDO::PARAM_STR);
            $stmt->execute();
        }

从我的班级成分我的相关方法:

public function __construct(array $data)
    {
    $this->hydrate($data);
    }

    public function hydrate(array $data)
    {
        $keys = array_keys($data);
        if (array_key_exists(0, $keys)) {
            $this->setCode($data[$keys[0]]);
            $this->setCodealias($keys[0]);
        }
        if (array_key_exists(1, $keys)) {
            $this->setName($data[$keys[1]]);
            $this->setNamealias($keys[1]);
        }
        if (array_key_exists(2, $keys)) {
        $this->setSupplier($data[$keys[2]]);
        $this->setSupplieralias($keys[2]);
        }
        if (array_key_exists(2, $keys)) {
        $this->setEditdate($data[$keys[3]]);
        $this->setEditdatealias($keys[3]);
        }
    }

控制器中的相关代码:

case "deleteIgr":
        require("Class/Ingredient.php");
        require("Class/IngredientsManager.php");
        $data = array ('code'=> $_POST['code']);
        $deleterIngredient = new IngredientsManager;
        $deletedIngredient=$deleterIngredient->deleted(new Ingredient($data));
        echo 'ok';

我确切地说我的$ _POST ['code']来自AJAX并且它在一个值代码中,当我做颜色日志并且这个值正在传递时,我不知道这个语法错误来自哪里。这是我第一次使用存储过程。

php sql-server stored-procedures pdo
1个回答
1
投票

您可以尝试使用以下方法之一执行存储过程:

$stmt = $db->prepare("{CALL DeleteIngredient(?)}");
$code = $ingredient->code();
$stmt->bindParam(1, $code);
$stmt->execute();

$stmt = $db->prepare("{CALL DeleteIngredient(:code)}");
$code = $ingredient->code();
$stmt->bindParam(':code', $code);
$stmt->execute();

$stmt = $db->prepare("EXEC DeleteIngredient @IdIng = :code");
$code = $ingredient->code();
$stmt->bindParam(':code', $code);
$stmt->execute();

我可以使用PHP驱动程序为SQL Server重现此错误,原因是{}周围缺少CALL DeleteIngredient(?)

© www.soinside.com 2019 - 2024. All rights reserved.