0

我正在尝试在 drupal 中运行 db 查询,其中内容类型具有节点关联字段,并且我正在尝试获取该类型的所有注释,其中当前节点的 NID 与所述注释关联中指定的任何节点匹配场地。

一个视觉例子

Nodetype1 -- 节点关联字段

节点类型2

我想获取节点关联字段与当前加载的 Nodetype2 的 NID 匹配的所有 Nodetype1。

我当前的数据库查询是这样的:

db_query("SELECT * FROM field_data_field_promo_profile WHERE field_promo_profile_nid=".$N->nid);

这什么也不返回,当我知道这样一个节点存在时,我也尝试删除 WHERE 语句,它返回一个像这样的数组:

DatabaseStatementBase Object ( [dbh] => DatabaseConnection_mysql Object ( [shutdownRegistered:protected] => [target:protected] => default [key:protected] => default [logger:protected] => [transactionLayers:protected] => Array ( ) [driverClasses:protected] => Array ( [SelectQuery] => SelectQuery [DatabaseSchema] => DatabaseSchema_mysql [MergeQuery] => MergeQuery [DatabaseTransaction] => DatabaseTransaction [UpdateQuery] => UpdateQuery [InsertQuery] => InsertQuery_mysql ) [statementClass:protected] => DatabaseStatementBase [transactionSupport:protected] => 1 [transactionalDDLSupport:protected] => [temporaryNameIndex:protected] => 0 [connectionOptions:protected] => Array ( [database] => cityhound_dev [username] => blahblah [password] => blahblah [host] => localhost [port] => [driver] => mysql [prefix] => Array ( [default] => ) ) [schema:protected] => DatabaseSchema_mysql Object ( [connection:protected] => DatabaseConnection_mysql Object *RECURSION* [placeholder:protected] => 0 [defaultSchema:protected] => public [uniqueIdentifier:protected] => 4fd7fba9e563e2.50177866 ) [prefixes:protected] => Array ( [default] => ) [prefixSearch:protected] => Array ( [0] => { [1] => } ) [prefixReplace:protected] => Array ( [0] => [1] => ) ) [queryString] => SELECT * FROM field_data_field_promo_profile )

任何人有一些想法?

4

2 回答 2

0

db_query()返回一个可迭代的对象,所以你只需要迭代它:

$result = db_query("SELECT * FROM field_data_field_promo_profile WHERE field_promo_profile_nid=".$N->nid);

foreach ($result as $row) {
  $entity_id = $row->entity_id;
  // etc...
}
于 2012-06-13T09:33:08.560 回答
0

您应该在查询中使用参数来防止 SQL 注入。

例如上面的查询应该是这样的:


$result = db_query("SELECT * FROM {field_data_field_promo_profile} p
   WHERE p.field_promo_profile_nid = :nid ", array(':nid' => $N->nid);

foreach ( $result as $row ) {
  $entity_id = $row->entity_id;
  // etc...
}

于 2013-02-24T02:00:49.590 回答