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

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

问题描述

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

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

我正在通过以下方式调用查询:

  $ query = 

SELECT node.nid AS projectnid
FROM节点节点
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);

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



所以我的问题将是如何让db_query返回你所有的结果作为一个对象(我不' t / b>

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



这是一个后续问题: Drupal视图关系和参数


我有一个Person内容类型。它有一个公司

节点引用字段,它也是内容类型。然后
有一个名为Project的内容类型。 A
项目有一个
公司内容类型的节点引用。我想列出
所有与一个人相关的项目
id(nid)id(nid)



解决方案

以下作品:

  $ query = 

SELECT node.nid AS projectnid
FROM节点节点
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节点节点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);
}

您需要使用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天全站免登陆