Null Coalesce Operator和SQL IN子句

时间:2017-09-12 17:15:49

标签: php mysql in-clause

我的主页显示的是一张表,其中包含根据当前用户团队从数据库中获取的不同数据。当用户登录时,我正在与团队一起创建一个cookie,在显示表时我正在阅读。

当用户未登录时,我还想显示该表,但我希望它显示2个(或更多)团队的数据。我正在使用临时解决方案,现在使用null coalesce运算符,默认为第一个团队,如下所示:$team = $_COOKIE ['team'] ?? 1;

我的查询:$associates = "SELECT associate_id, first_name, last_name FROM scp.associates WHERE team = '$team' ORDER BY associate_id ASC";

有没有办法修改其中一个或两个以获得我想要的输出?到目前为止,我尝试了以下内容:

$team = $_COOKIE ['team'] ?? '1, 2';
$associates = "SELECT associate_id, first_name, last_name FROM scp.associates
    WHERE team IN ('$team') ORDER BY team ASC, associate_id ASC";

如果设置了cookie,则有效:

$team = $_COOKIE ['team'] ?? "'1', '2'";
$associates = "SELECT associate_id, first_name, last_name FROM scp.associates
    WHERE team IN ($team) ORDER BY team ASC, associate_id ASC";

在未设置cookie时有效...我尝试了其他变体,但无法使其正常工作。有任何想法吗?谢谢!

编辑:我的Cookie是一个字符串,我现在正在使用预准备语句。新代码如下所示:

$team = $_COOKIE['team'] ?? '1';
$associates = "SELECT associate_id, first_name, last_name FROM scp.associates
    WHERE team IN (?) ORDER BY team ASC, associate_id ASC";
$res_associates = odbc_prepare ( $conn, $associates );
odbc_execute ( $res_associates, array ( $team ) );

当我更改为'1, 2'时,我没有收到数据库的结果。我的if ( odbc_num_rows ( $res_associates ) > 0 )是假的。

Edit2 :当我在查询中直接添加值时,它可以正常运行,但是当它从变量中获取时(无论是否已准备好)它都不会......

这样可行:

$associates = "SELECT associate_id, first_name, last_name FROM scp.associates
    WHERE team IN ('1', '2') ORDER BY team ASC, associate_id ASC";

但这不是:

$team = $_COOKIE['team'] ?? " '1', '2'";
$associates = "SELECT associate_id, first_name, last_name FROM scp.associates
    WHERE team IN (?) ORDER BY team ASC, associate_id ASC";

(需要“和”之间的空格,因此它不认为它是文件)

解决方案

$team = $_COOKIE['team'] ?? '1,2';
$terms = explode ( ',', $team );
$placeholders = rtrim ( str_repeat ( '?, ', count ( $terms ) ), ', ' );

$associates = "SELECT associate_id, first_name, last_name FROM scp.associates
    WHERE team IN ($placeholders) ORDER BY team ASC, associate_id ASC";

$res_associates = odbc_prepare ( $conn, $associates );
odbc_execute ( $res_associates, $terms );

2 个答案:

答案 0 :(得分:3)

您应该在,上拆分,将占位符放入查询中,然后绑定每个术语。

类似的东西(我假设你正在使用PDO,如果没有清空execute调用并使用适当的调用你的驱动程序),这样就可以了:

$team = $_COOKIE['team'] ?? '1, 2';
$terms = explode(',', $team);
$placeholders = rtrim(str_repeat('?, ', count($terms)), ', ');
$associates = "SELECT associate_id, first_name, last_name 
               FROM scp.associates 
               WHERE team IN ($placeholders) 
               ORDER BY team ASC, associate_id ASC";
$get_stuff = $pdo->prepare($associates);
$get_stuff->execute($terms)); 

粗略演示:https://3v4l.org/REqPc

答案 1 :(得分:0)

这可能是一个类型问题,在解决此类问题时需要考虑一些问题。什么类型$_COOKIE ['team']?数据库中的team是什么类型的?

选项1:我认为PHP可能认为$_COOKIE ['team']是一个int,它应该是一个字符串,它需要引号。所以你可以做这样的事情,它隐式地将它强制转换为字符串并添加引号:

$team = $_COOKIE ['team'] ? "'" . $_COOKIE ['team'] . "'" : "'1', '2'";

选项2:在查询中添加引号,如第一个中所示 WHERE team IN ('$team') 然后只是把它变成一个字符串。

$team = $_COOKIE ['team'] ? (string) $_COOKIE ['team'] : '1, 2';