php - 值从 php 到存储过程

标签 php mysql stored-procedures

这是我的PHP代码

<?php
$code='1,2,3';
$this->db->query("CALL SP_db(\"$code\"));
?>

如果我 var_dump($code) 我得到这个 string(5) "1,2,3"

这是我的存储过程代码

CREATE DEFINER=`test`@`localhost` PROCEDURE `SP_db`(v_company varchar(10))

begin

select * from blabla where company_id in (v_company);
.
.
.

我使用这段代码向表中插入一些值 如果我使用这段代码的问题,我从 php 获取 v_company 它不起作用

但如果我尝试填充它起作用的值

CREATE DEFINER=`test`@`localhost` PROCEDURE `SP_db`(v_company varchar(10))

    begin

    select * from blabla where company_id in (1,2,3);

但 php 发送的值由存储过程接收, 我尝试将 v_company 值保存到表中以确保它有效

因为php的值进入了表

谁能告诉我代码哪里错了

最佳答案

当你将 '1,2,3' 传递给你的过程时,你会得到一个字符串,你的查询就变成了

select * from blabla where company_id in ('1,2,3');

不是

select * from blabla where company_id in (1,2,3);

您可以使用find_in_set代替in

select * from blabla where find_in_set(company_id, v_company);

但它不会像in那样很好地使用索引;

关于php - 值从 php 到存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13622509/

相关文章:

azure - DocumentDB,如何在 SP 中使用 continuationToken

php - 如何在 CodeIgniter 中使用相对路径?

不带引号的 PHP 数组访问

php - 更新查询不更新表

mysql - 显示之间的所有日期,即使没有结果

MySQL存储过程?

c# - SQL Server 2005 中 .NET 存储过程的默认参数值/可选参数

php - 自定义 Artisan 命令以执行多个命令

mysql - 使用 And 语句进行多重连接不起作用

php - 使用 php 和 json 的 MySql 产品和类别表