How does one set cookies in WordPress without getting the ‘headers not sent’ error? within WP

The solution is setting a function that’s hooked into init.

add_action('init', 'stats_cookie');
function stats_cookie()
{
    if(!isset($_COOKIE['site_stats']))
    { 
        $current = current_time( 'timestamp', 1);
        setcookie('site_stats', $current, time()+60+60*24*7, COOKIEPATH, COOKIE_DOMAIN ); 
    }
}

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