How do you get parent and grandparent categories [duplicate]

This query will help you

global $wpdb;

$cat_id = 65;

// this query will get parent relationships from term_taxonomy table and get category names from terms table
$category = $wpdb->get_row(
    "SELECT t4.term_id as parent_id, t4.name as parent_name, t5.term_id as grandparent_id, t5.name as grandparent_name FROM `{$wpdb->prefix}term_taxonomy` t1
left join `{$wpdb->prefix}term_taxonomy` t2 on t2.term_id = t1.parent
left join `{$wpdb->prefix}term_taxonomy` t3 on t3.term_id = t2.parent
left join `{$wpdb->prefix}terms` t4 on t4.term_id = t2.term_id
left join `{$wpdb->prefix}terms` t5 on t5.term_id = t3.term_id
where t1.term_id={$cat_id}"
);

if ($category->grandparent_id) {
    echo "blog > {$category->grandparent_name}  > {$category->parent_name}";
} else if ($category->parent_id) {
    echo "blog > {$category->parent_name}";
}

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)