0

PDO 没有显示任何数据,有人可以帮助我:

$ClanData_users = sql::db()->prepare('SELECT * FROM clan_game INNER JOIN clan_page ON clan_game.clan_home = clan_page.id ORDER BY clan_game.date ASC');
$ClanData_users->execute();
$q = ($ClanData_users);

$content .= '<table class="forum"><tbody><tr class="bericht-content"><td> Kalender</td></tr>';
while($r = $q->fetch()){
    $content .= '<tr><td><span class="beschrijving"><p> '.$r['clan_game.clan_home'].' '.$r['clan_challenger'].' '.ucfirst(strftime("%A %H <b>%B</b> %Y | %R", strtotime($r['date']))).'  </p></span></td></tr> ';
}
$content .= '</tbody></table>';
4

2 回答 2

2

SELECT *在查询中通常不是一个好主意JOIN。相反,请具体说明您需要的列,并在名称重叠时为它们分配别名。获取后无法通过表名访问它们$r['table_name.column_name']。关联列只能$r['column_name']通过列名或别名使用,因此所有列名/别名必须是唯一的。

$ClanData_users = sql::db()->prepare('
  SELECT 
    /* If both tables have the same column name, use an alias */
    clan_game.id AS clan_game_id,
    clan_home.id AS clan_home_id,
    clan_game.clan_home AS clan_home,
    clan_challenger,
    /* etc... Be specific and alias as necessary */
  FROM clan_game 
    INNER JOIN clan_page ON clan_game.clan_home = clan_page.id
  ORDER BY clan_game.date ASC');

提取时,仅使用列名或别名

// Why reference $ClanData_users as $q here? That's confusing. Just fetch from $ClanData_users
while($r = $ClanData_users->fetch(PDO::FETCH_ASSOC)){
    $content .= '<tr><td><span class="beschrijving"><p> '.$r['clan_home'].' '.$r['clan_challenger'].' '.ucfirst(strftime("%A %H <b>%B</b> %Y | %R", strtotime($r['date']))).'  </p></span></td></tr> ';
}
于 2012-10-16T13:36:34.677 回答
0

如果$r为假,则查询不成功。在这一点上,我们无法判断您的代码有什么问题,因为我们没有您的数据库模式。SQL 语法对我来说似乎很好。了解问题所在的唯一方法是尝试在 PhpMyAdmin 中运行查询。

于 2012-10-16T14:02:36.887 回答