Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Drupal7中的数据库查询_Mysql_Drupal_Drupal 7 - Fatal编程技术网

Mysql Drupal7中的数据库查询

Mysql Drupal7中的数据库查询,mysql,drupal,drupal-7,Mysql,Drupal,Drupal 7,我试图在drupal中运行db查询,其中,内容类型有一个节点关联字段,我试图获取该类型的所有注释,其中当前节点的NID匹配在所述注释关联字段中指定的任何节点 一个直观的例子 Nodetype1 --节点关联字段 NodeType2 我想获取所有Nodetype1的where节点关联字段与当前加载的Nodetype2的NID匹配 我当前的数据库查询如下所示: db_query("SELECT * FROM field_data_field_promo_profile WHERE field_pro

我试图在drupal中运行db查询,其中,内容类型有一个节点关联字段,我试图获取该类型的所有注释,其中当前节点的NID匹配在所述注释关联字段中指定的任何节点

一个直观的例子

Nodetype1 --节点关联字段

NodeType2

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

我当前的数据库查询如下所示:

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 )

$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...
}
有人有什么想法吗?

db\u query()
返回一个iterable对象,因此您只需对其进行迭代:

$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...
}

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

例如,上面的查询应该如下所示:

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 )

$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...
}

好的,我将尝试这个,我如何测试结果是否为空,因为即使它不返回任何内容,它也不是空数组,当它为空时,它会到处抛出错误。看,我尝试了这个,它返回了db错误,可能是因为我查询了字段和节点的内容类型吗?