PDO is not displaying any data could somebody help me out:
$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>';
It usually isn't a good idea to SELECT *
in a JOIN
query. Instead, be specific about the columns you need, and assign aliases to them when the names overlap. You cannot access them with the table name via $r['table_name.column_name']
after fetching. The associative columns will only be available in $r['column_name']
by the column name or alias, so all column names/aliases must be unique.
$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');
When fetching, use only the column name or alias
// 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> ';
}
If $r
is false then the query did not succeed. At this point we cannot tell what's wrong with your code since we do not have your database schema. The SQL syntax seems fine to me. The only way to understand where the problem relies is try running the query within PhpMyAdmin.