简体   繁体   English

PHP PDO:如何调用存储过程以逗号分隔的字符串传递给参数

[英]PHP PDO : How to call stored procedure to pass comma separated string to parameter

I have worked out a stored procedure with a prepared statements inside to pass a comma separated string to one of the parameters with help from another thread . 我已经设计出了一个存储过程,该存储过程中包含一个准备好的语句,可以在另一个线程的帮助下将逗号分隔的字符串传递给其中一个参数。 Now I'm having trouble binding values to positional parameters in PHP PDO. 现在,我在将值绑定到PHP PDO中的位置参数时遇到了麻烦。 I need to call the stored procedure like this: 我需要这样调用存储过程:

CALL load_things('''1283943kd9'',''2e9kk389334''','53')

The first parameter is a list of model numbers and the second one 53 is an user id. 第一个参数是型号列表,第二个参数53是用户ID。 It is workings fine when I enter the command in Adminer or phpmyadmin. 当我在Adminer或phpmyadmin中输入命令时,它工作正常。

Here's the PHP code to get the corresponding number of question marks into the brackets: 这是将相应数量的问号放入括号中的PHP代码:

 $id_group = $_POST["group"]; // array

 $in  = str_repeat("''?'',", count($id_group) - 1) . "''?''";    

 $sql .= "call loadit('".$in."','?')";

 $users = $dbh->prepare($sql);

 $i = 1;

 foreach ($id_group as $id) 
 {
   $users->bindValue($i++, $id);
 }

 $lasti = (count($id_group) + 1);

 $users->bindValue($lasti,$_SESSION["user_id"]);

 $users->execute();

On a page that sends out 20 values in $_POST["group"] , (each of them is 30 characters max) it seems to have generated the exact number of placeholders from the look of the following output. 在发出$_POST["group"] 20个值的页面上(每个值最多30个字符),根据以下输出的外观,似乎已经生成了精确的占位符数。 (The 21 st is the user id), but I'm not getting any results. (第21个是用户ID),但没有得到任何结果。 Here's the response from Chrome console`: 以下是Chrome控制台的响应:

SQL: [143] 

call loadit('''?'',''?'',''?'',''?'',''?'',''?'',''?'',''?'',''?'',''?'',''?'',''?'',''?'',''?'',''?'',''?'',''?'',''?'',''?'',''?''','?')

Params:  21
Key: Position #0:
paramno=0
name=[0] ""
is_param=1
param_type=2
Key: Position #1:
paramno=1
name=[0] ""
is_param=1
param_type=2
Key: Position #2:
paramno=2
name=[0] ""
is_param=1
param_type=2
Key: Position #3:
paramno=3
name=[0] ""
is_param=1
param_type=2
Key: Position #4:
paramno=4
name=[0] ""
is_param=1
param_type=2
Key: Position #5:
paramno=5
name=[0] ""
is_param=1
param_type=2
Key: Position #6:
paramno=6
name=[0] ""
is_param=1
param_type=2
Key: Position #7:
paramno=7
name=[0] ""
is_param=1
param_type=2
Key: Position #8:
paramno=8
name=[0] ""
is_param=1
param_type=2
Key: Position #9:
paramno=9
name=[0] ""
is_param=1
param_type=2
Key: Position #10:
paramno=10
name=[0] ""
is_param=1
param_type=2
Key: Position #11:
paramno=11
name=[0] ""
is_param=1
param_type=2
Key: Position #12:
paramno=12
name=[0] ""
is_param=1
param_type=2
Key: Position #13:
paramno=13
name=[0] ""
is_param=1
param_type=2
Key: Position #14:
paramno=14
name=[0] ""
is_param=1
param_type=2
Key: Position #15:
paramno=15
name=[0] ""
is_param=1
param_type=2
Key: Position #16:
paramno=16
name=[0] ""
is_param=1
param_type=2
Key: Position #17:
paramno=17
name=[0] ""
is_param=1
param_type=2
Key: Position #18:
paramno=18
name=[0] ""
is_param=1
param_type=2
Key: Position #19:
paramno=19
name=[0] ""
is_param=1
param_type=2
Key: Position #20:
paramno=20
name=[0] ""
is_param=1
param_type=2
[]

Stored Procedure and table schema ( fiddle ): 存储过程和表模式( 小提琴 ):

DELIMITER ;;
CREATE PROCEDURE `load_things` (IN `yr_model_no` varchar(1000), IN `yr_app_id` int(5))
BEGIN
    SET @s = 

CONCAT('
SELECT * FROM 
(
   SELECT COUNT( c.app_id ) AS users_no, ROUND( AVG( c.min ) , 1 ) AS avg_min, ROUND( AVG( c.max ) , 1 ) AS avg_max, a.mid, a.likes, a.dislikes, b.model_no
        FROM  `like` a
        RIGHT JOIN  `model` b ON a.mid = b.mid
        LEFT JOIN  `details` c ON c.mid = b.mid
        WHERE b.model_no IN (',yr_model_no,')
        GROUP BY b.model_no
)TAB1
JOIN
(
   SELECT a.app_id,b.model_no,IFNULL(c.isbooked,0) AS isbooked,d.min,d.max,e.like_type
     FROM `users` a
     JOIN `model` b
       ON b.model_no IN (',yr_model_no,')
LEFT JOIN `favorite` c 
       ON c.app_id = a.id
      AND c.mid = b.mid         
LEFT JOIN `details` d 
       ON d.app_id = a.id
      AND d.mid = b.mid 
LEFT JOIN `users_likes` e
       ON e.app_id = a.id
      AND e.mid = b.mid
    WHERE a.id = ',yr_app_id,'
)TAB2
ON TAB1.model_no = TAB2.model_no');

PREPARE stmt from @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt3;
END;;
DELIMITER ;

Can anyone help me figure out what's wrong with the PHP code? 谁能帮我弄清楚PHP代码有什么问题吗? Is my stored procedure not compatible with PDO? 我的存储过程与PDO不兼容吗?

You're quoting your placeholders. 您正在引用占位符。 That's a no-no. 那是不可以的。 The DB and the interface library will take care of all that themselves. 数据库和接口库将自行处理所有工作。 All you need is to provide the placeholders themselves: 您所需要做的就是自己提供占位符:

$in  = str_repeat('?,' count($id_group) - 1) . '?';

which should eventually produce 最终应该产生

call loadit(?,?,?,......?)

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

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