Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中使用OUT参数调用存储过程_Php_Mysql_Stored Procedures - Fatal编程技术网

在PHP中使用OUT参数调用存储过程

在PHP中使用OUT参数调用存储过程,php,mysql,stored-procedures,Php,Mysql,Stored Procedures,使用IN参数调用存储过程很容易 CREATE PROCEDURE `catalog_delete_product`(IN `inProductId` INT) BEGIN DELETE FROM product_attribute WHERE product_id = inProductId; DELETE FROM product_category WHERE product_id = inProductId; DELETE FROM shopping_cart WHERE product_i

使用IN参数调用存储过程很容易

CREATE PROCEDURE `catalog_delete_product`(IN `inProductId` INT) BEGIN DELETE FROM product_attribute WHERE product_id = inProductId; DELETE FROM product_category WHERE product_id = inProductId; DELETE FROM shopping_cart WHERE product_id = inProductId; DELETE FROM product WHERE product_id = inProductId; END

你可以看到,事情就这么简单。但是我们如何在MySQL存储参数中调用OUT参数并在PHP中使用它呢

作为一个示例,我将介绍一个实际的示例(将数据插入订单表并返回lastInsertId)

在PHP级别//可能首先在模型/实体级别,我们需要执行

购物车创建订单()

存储过程。这可能在函数中

其次,要获取最后一个订单id,我们需要从变量中查询它

@老年人

。我们必须调用该方法,这一点很重要

closeCursor()

为了执行下一个SQL语句,需要调用PDOStatement对象的

function query($pdo, $sql, $parameters = []){
    $query = $pdo->prepare($sql);
    $query->execute($parameters);
    return $query;
}

function create_order($pdo, $cart_id){
    // Binding the parameters
    $parameters = [':cart_id' => $cart_id];

    // calling stored procedure command
    $sql = 'CALL shopping_cart_create_order(:cart_id)';

    // prepare for execution of the stored procedure, pass value to the command 
    and execute the Stored Procedure
    $query = query($pdo, $sql, $parameters);

   // Then close Cursor. It is important for you to close it.
    $query->closeCursor();

   // execute the second query to get last insert id
    $row = $pdo->query("SELECT @oid AS oid")->fetch();
    return $row;    
}

@奈杰尔·伦:嗯,我搜索了又搜索,但在这里或任何地方都找不到答案,直到我偶然发现了mysqltutorial。但是如果社区认为它是重复的,那么我可以删除它。如果你想删除它,这取决于你,因为有些人可能会发现它很有用。不使用参数,只需在过程结束时选择变量,你就可以像使用任何其他结果集一样使用PHP中的结果集。这样可以节省执行另一个查询的时间。当您从另一个过程调用一个过程时,OUT参数很有用,而不是将信息传递回应用程序时。@slaakso这就是我以前所做的。它在本地主机上工作,但在实时服务器上不工作本地服务器和远程(实时)服务器之间没有区别。如果您可以连接到服务器,那么它们应该与PHP一样工作。如果一台服务器出现问题,您可以从中提出问题。
function query($pdo, $sql, $parameters = []){
    $query = $pdo->prepare($sql);
    $query->execute($parameters);
    return $query;
}

function create_order($pdo, $cart_id){
    // Binding the parameters
    $parameters = [':cart_id' => $cart_id];

    // calling stored procedure command
    $sql = 'CALL shopping_cart_create_order(:cart_id)';

    // prepare for execution of the stored procedure, pass value to the command 
    and execute the Stored Procedure
    $query = query($pdo, $sql, $parameters);

   // Then close Cursor. It is important for you to close it.
    $query->closeCursor();

   // execute the second query to get last insert id
    $row = $pdo->query("SELECT @oid AS oid")->fetch();
    return $row;    
}