在 views_php 中的 db_query 中使用 entity_id (Drupal 7)

Using entity_id in a db_query in views_php (Drupal 7)

我在 views_php 中创建了以下代码:

$userseeking = db_query("SELECT field_seeking_tid FROM {field_data_field_seeking} WHERE entity_id = 6")->fetchAll(PDO::FETCH_ASSOC);
$userseekingcount = count($userseeking);
return $userseekingcount

在上面的示例中,我为 entity_id 使用了特定值“6”。我收到了此查询的预期结果。

但是,entity_id 需要是一个依赖于登录用户的配置文件 ID(与其用户 ID 相关联)的变量。

因此我介绍了 3 行来从当前用户检索 PID ($currentpid)。我已经确认这 3 行 return 的 $currentpid 值为 6。当我将 db_query 修改为 select 值时 entity_id = $currentpid,似乎有失败,尽管它们应该产生相同的结果 - 请参见下面的代码:

global $user;
$currentuser = $user->uid;
$currentpid = db_query('SELECT pid FROM {profile} WHERE uid = ' . $currentuser . ' limit 1')->fetchField();

$userseeking = db_query("SELECT field_seeking_tid FROM {field_data_field_seeking} WHERE entity_id = ' . $currentpid . '")->fetchAll(PDO::FETCH_ASSOC);
$userseekingcount = count($userseeking);
return $userseekingcount

我是否误用了此查询中引用 entity_id 的方式?

感谢您的帮助!

PS 这可能不是最干净的代码,所以我提前道歉! :)

原来在上面的例子中存在语法错误。特别是我在创建 $userseeking 时使用了 " 和 '。

希望其他人能从我的菜鸟错误中吸取教训:)

更正后的代码是:

global $user;
$currentuser = $user->uid;
$currentpid = db_query('SELECT pid FROM {profile} WHERE uid = ' . $currentuser . ' limit 1')->fetchField();

$userseeking = db_query('SELECT field_seeking_tid FROM {field_data_field_seeking} WHERE entity_id = ' . $currentpid . '')->fetchAll(PDO::FETCH_ASSOC);
$userseekingcount = count($userseeking);
return $userseekingcount