Drupal 对数据库执行查询 [英] Drupal performing a query against the database

查看:32
本文介绍了Drupal 对数据库执行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从我的 drupal 数据库中检索一些 nid.我有一个想要运行的查询.

 SELECT node.nid AS projectnidFROM 节点 节点INNER JOIN content_type_project node_data_field_project_client ON node.vid = node_data_field_project_client.vidWHERE node_data_field_project_client.field_project_client_nid = (SELECT node_data_field_profile_company.field_profile_company_nid AS company_nidFROM node node LEFT JOIN content_type_profile node_data_field_profile_company ON node.vid = node_data_field_profile_company.vid WHERE node.nid = 218)

我使用以下方法调用查询:

$query ="SELECT node.nid AS projectnidFROM 节点 节点INNER JOIN content_type_project node_data_field_project_client ON node.vid = node_data_field_project_client.vidWHERE node_data_field_project_client.field_project_client_nid = (SELECT node_data_field_profile_company.field_profile_company_nid AS company_nidFROM node node LEFT JOIN content_type_profile node_data_field_profile_company ON node.vid = node_data_field_profile_company.vid WHERE node.nid = 218)";$result = db_query($query);dsm($result);

dsm 给了我一个空对象.当我直接运行 SQL 时,我会得到一个结果.

所以我的问题是如何让 db_query 将所有结果作为对象返回(我真的不介意是对象还是数组).

(SQL 是通过查看视图的查询输出创建的.)

这是对问题的跟进:Drupal 视图关系和参数><块引用>

我有一个 Person 内容类型.它有一个公司节点引用字段这也是一种内容类型.然后我有一个名为 Project 的内容类型.一个项目有一个节点引用公司内容类型.我要列出与一个人有关的所有项目id (nid)id (nid)

解决方案

以下作品:

$query ="SELECT node.nid AS projectnidFROM 节点 节点INNER JOIN content_type_project node_data_field_project_client ON node.vid = node_data_field_project_client.vidWHERE node_data_field_project_client.field_project_client_nid = (SELECT node_data_field_profile_company.field_profile_company_nid AS company_nidFROM node node LEFT JOIN content_type_profile node_data_field_profile_company ON node.vid = node_data_field_profile_company.vid WHERE node.nid = 218)";$results = db_query($query);而 ($result = db_result($results)) {dsm($result);}

您需要使用 db_result() 来获取结果.使用 http://drupal.org/node/259432#comment-846946 解决了这个问题一个>

I wish to retrieve some nids from my drupal database. I have a query that I wish to run.

  SELECT node.nid AS projectnid
            FROM node node
            INNER JOIN content_type_project node_data_field_project_client ON node.vid = node_data_field_project_client.vid
            WHERE node_data_field_project_client.field_project_client_nid = (SELECT node_data_field_profile_company.field_profile_company_nid AS company_nid
            FROM node node  LEFT JOIN content_type_profile node_data_field_profile_company ON node.vid = node_data_field_profile_company.vid WHERE node.nid = 218)

I am calling the query by using:

$query =
        "
            SELECT node.nid AS projectnid
            FROM node node
            INNER JOIN content_type_project node_data_field_project_client ON node.vid = node_data_field_project_client.vid
            WHERE node_data_field_project_client.field_project_client_nid = (SELECT node_data_field_profile_company.field_profile_company_nid AS company_nid
            FROM node node  LEFT JOIN content_type_profile node_data_field_profile_company ON node.vid = node_data_field_profile_company.vid WHERE node.nid = 218)
        ";
$result = db_query($query);
dsm($result);

The dsm is giving me an empty object. When I run the SQL directly I get back a result.

So my question would be how would you get db_query to return you all your results as an object (I don't really mind if an object or array).

(The SQL was created by looking at the query output for views.)

This is a follow up to question: Drupal Views Relationships and Arguments

I have a Person content type. It has a node reference field of a company which is also a content type. I then have a content type called Project. A project has a node reference to a company content type. I want to list all the projects related to a person id (nid)id (nid)

解决方案

The following works:

$query =
        "
            SELECT node.nid AS projectnid
            FROM node node
            INNER JOIN content_type_project node_data_field_project_client ON node.vid = node_data_field_project_client.vid
            WHERE node_data_field_project_client.field_project_client_nid = (SELECT node_data_field_profile_company.field_profile_company_nid AS company_nid
            FROM node node  LEFT JOIN content_type_profile node_data_field_profile_company ON node.vid = node_data_field_profile_company.vid WHERE node.nid = 218)
        ";
$results = db_query($query);
while ($result = db_result($results)) {
    dsm($result);
}

You need to use db_result() to get the results out. Worked this out by using http://drupal.org/node/259432#comment-846946

这篇关于Drupal 对数据库执行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆