简体   繁体   English

接受多个参数的存储过程

[英]Stored procedure that accepts multiple parameters

It's my first time working with stored procedures. 这是我第一次使用存储过程。

The previous developer already had a stored procedure in place that works, but it only accepts 1 parameter. 以前的开发人员已经有一个可以使用的存储过程,但是它仅接受1个参数。

I am using PHP to pass the parameters: 我正在使用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)");
 ?>

I can send 1 value with no problem. 我可以毫无问题地发送1个值。 I get back the data. 我取回数据。 But when there are more than 1, I get the following error: 但是,当数量超过1时,会出现以下错误:

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

Here is what the stored procedure looks like (shortened for time): 这是存储过程的样子(缩短了时间):

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

The parameter cont is varchar(11) // not sure if that means anything 参数cont是varchar(11)//不知道这是否意味着什么

This is my first attempting a stored procedure call, and I can return data for one value. 这是我第一次尝试存储过程调用,并且我可以返回一个值的数据。 I need to return data for multiple values. 我需要返回多个值的数据。

The error message is absolutely right. 错误消息是绝对正确的。 You are sending 3 parameters to a stores procedure which takes only one. 您正在将3个参数发送到仅需一个的存储过程。

What you've done is you have modified the stored proc which takes a single string such that it still expects a single string. 您所做的就是修改了存储过程,该过程采用单个字符串,因此仍希望使用单个字符串。

You should modify the definition of the stored procedure to take 3 parameters (that part is missing in your question) 您应该修改存储过程的定义以采用3个参数(问题中缺少该部分)

Here is an example of a stored proc declaration with 3 parameters: 这是带有3个参数的存储proc声明的示例:

 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

You should also change your code to use parameterized queries instead of the way you're doing right now. 您还应该更改代码以使用参数化查询,而不是现在的方式。 See: http://php.net/manual/en/pdo.prepared-statements.php or http://php.net/manual/en/mysqli.prepare.php 参见: http : //php.net/manual/en/pdo.prepared-statements.phphttp://php.net/manual/en/mysqli.prepare.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM