为什么db_placeholder为我的查询返回空字符串?

时间:2010-11-20 21:39:04

标签: drupal drupal-6

我在查询中使用db_placeholder来替换字符串数组。

$paths = array("commentary_analysis/18", "commentary_analysis/16", "commentary_analysis/95", "commentary_analysis/89");

$query = "SELECT DISTINCT a.uid, u.mail FROM {alerts} a JOIN {users} u on u.uid = a.uid WHERE u.mail IS NOT NULL AND u.mail != '' AND u.status = 1 AND a.status = %d AND a.view_path IN (" . db_placeholders($paths, 'text') . ") ORDER BY a.uid ASC";

$users_to_notify = db_query($query, $alert_status, $paths);

当我知道它应该时,查询不会返回结果。我调试了查询以查看正在运行的确切查询。正在运行的查询是,

SELECT DISTINCT a.uid, u.mail FROM alerts a JOIN users u on u.uid = a.uid WHERE u.mail IS NOT NULL AND u.mail != '' AND u.status = 1 AND a.status = 1 AND a.view_path IN ('','','','') ORDER BY a.uid ASC

注意,a.view_path IN ('','','','')。那就是问题所在。从我所看到的,我期望db_placeholder正在使用正确的语法。

有人可以告诉我为什么我使用db_placeholder会返回('','','','')而不是("commentary_analysis/18", "commentary_analysis/16", "commentary_analysis/95", "commentary_analysis/89")吗?

3 个答案:

答案 0 :(得分:4)

问题是您可以将所有单值参数传递给db_query,也可以传递参数数组。你的代码传递了一个值,然后是一个数组,而Drupal就不那么灵活了。

试试这个:

$paths = array(
  'commentary_analysis/18',
  'commentary_analysis/16',
  'commentary_analysis/95',
  'commentary_analysis/89',
);

$query = "SELECT DISTINCT a.uid, u.mail
          FROM {alerts} a
          JOIN {users} u on u.uid = a.uid
          WHERE u.mail IS NOT NULL
            AND u.mail != ''
            AND u.status = 1
            AND a.status = %d
            AND a.view_path IN (" . db_placeholders($paths, 'varchar') . ")
          ORDER BY a.uid ASC";

$args = array_merge(array($alert_status), $paths);
$users_to_notify = db_query($query, $args);

答案 1 :(得分:1)

在查询中交换db_query修饰符的顺序可以解决问题。

  $query = "SELECT DISTINCT a.uid, u.mail FROM {ifrmarkets_alerts} a JOIN {users} u on u.uid = a.uid WHERE u.mail IS NOT NULL AND u.mail != '' AND a.view_path IN (" . db_placeholders($paths, 'varchar') . ") AND u.status = 1 AND a.status = %d ORDER BY a.uid ASC";
  $users_to_notify = db_query($query, $paths, $alert_status);

答案 2 :(得分:-1)

首先,在Drupal 7中不推荐使用db_placeholders,因此您应该考虑不使用它。它是内存浪费功能。

尝试使用varchar数据类型:

$query = "SELECT DISTINCT a.uid, u.mail FROM {alerts} a JOIN {users} u on u.uid = a.uid WHERE u.mail IS NOT NULL AND u.mail != '' AND u.status = 1 AND a.status = %d AND a.view_path IN (" . db_placeholders($paths, 'varchar') . ") ORDER BY a.uid ASC"