gpt4 book ai didi

mysql - JOIN 在 SQL 中有效,但在 Drupal 中无效

转载 作者:行者123 更新时间:2023-11-29 23:40:08 25 4
gpt4 key购买 nike

我正在尝试为 Drupal 7.x 制作一个模块。在某个时刻我想使用 sql 查询(JOIN)。当我在 MYSQL 中尝试查询时,它起作用了。但是当我想在 Drupal 中尝试时,数组是空的。

所以我猜想sql查询和drupal查询之间是有区别的(可能是实现不同)。

SQL查询

SELECT * FROM friends 
INNER JOIN users
ON friends.uid=users.uid

Drupal实现

    function project_myfriends(){
// Use database API to retrieve tasks
$query = db_select('friends', 'f');

$query->join('users', 'u', 'f.uid = u.uid'); // JOIN

$query->fields('u', array('name'))
->execute();
return $query;
}

/**
* Implements hook_block_view().
*/
function project_block_view($delta = ''){
switch ($delta) {
case 'project':
$block['subject'] = t('My Friends');
// Use our custom function to retrieve data
$result = project_myfriends();

$items = array();

var_dump($result);
foreach($result as $friend){
$items[] = array(
'data' => $friend->name,
);
}
// No tasks
if (empty($items)) {
$block['content'] = t('No friends.');
}
else {
// Pass data trough theme function
$block['content'] = theme('item_list', array(
'items' => $items));
}
}
return $block;
}

提前致谢

最佳答案

您忘记获取结果查询:

$result = project_myfriends()->execute()->fetchAll();
var_dump($result);

关于mysql - JOIN 在 SQL 中有效,但在 Drupal 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26200319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com