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

时间:2017-01-22 18:13:32

标签: 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

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

但是,entity_id必须是一个变量,取决于登录用户的个人资料ID(连接到他们的用户ID)。

因此我引入了3行来从当前用户检索PID($ currentpid)。我已经确认这3行返回$ currentpid值为6.当我推荐db_query选择entity_id = $ currentpid的值时,似乎有失败,尽管它们应该产生相同的结果 - 请参阅下面的代码:< / p>

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这可能不是最干净的代码,所以我先生道歉! :)

1 个答案:

答案 0 :(得分:0)

事实证明在上面的例子中滥用了语法。特别是我使用&#34;和&#39;在创建$ 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