存储过程接受多个参数

时间:2017-07-21 15:23:12

标签: php stored-procedures

这是我第一次使用存储过程。

以前的开发人员已经有一个适用的存储过程,但它只接受1个参数。

我使用PHP传递参数:

 <?php
   $containers = $_POST['cntnum'];

   $shortened = array();
   foreach($containers as $short)
   {
     $shortened[] = substr($short, 0, 10);
   }
   $sans_check = preg_replace('/\n$/','',preg_replace('/^\n/','',preg_replace('/[\r\n]+/',"\n",$shortened)));
   $sans = "'" . implode("', '", $sans_check) ."'";

   // At this point, $sans looks like this: 'value1', 'value2', 'value3'... 

   // now I send $sans to the stored procedure
   $thecall = mysqli_query($dbc, "CALL SP_ContSearch_TEST($sans)");
 ?>

我可以毫无问题地发送1个值。我收回了数据。但是当有超过1个时,我会收到以下错误:

Incorrect number of arguments for PROCEDURE table.storeprocedure; expected 1, got 3

以下是存储过程的样子(缩短了时间):

Begin
  DECLARE sans_check varchar(100);  // adjusted from 10, but same error message
  SET sans_check = SUBSTR(cont,1,10);
  SELECT
    `inventory`
    ,delivery_date
    ,pool
  FROM 
    inventory
  WHERE
    CONTAINER_CHECK IN (cont);
  END

参数cont是varchar(11)//不确定这是否意味着什么

这是我第一次尝试存储过程调用,我可以返回一个值的数据。我需要为多个值返回数据。

1 个答案:

答案 0 :(得分:2)

错误消息绝对正确。您正在向存储过程发送3个参数,该过程只需要一个。

您所做的是您修改了存储过程,该过程采用单个字符串,因此它仍然需要一个字符串。

您应该修改存储过程的定义以获取3个参数(问题中缺少该部分)

以下是带有3个参数的存储过程声明的示例:

 CREATE PROCEDURE SP_ContSearch_TEST
    (IN sans1 CHAR(10),
     IN sans2 CHAR(10),
     IN sans3 CHAR(10)
     -- add as many other parameters here as you need
    )
 BEGIN
     -- your stored proc logic here.. can use sans1, sans2, and sans3
 END

您还应该更改代码以使用参数化查询,而不是您现在正在做的方式。请参阅:http://php.net/manual/en/pdo.prepared-statements.phphttp://php.net/manual/en/mysqli.prepare.php

相关问题