Welcome aboard Dpkb, first you have to allow remote access from the remote server. Generally this is accessed via some control panel (Cpanel, WHM, Plesk), you enter the "allowed remote hosts." You'd turn remote access on and enter site A as (one of, there can be multiple) allowed remote host.
Next is to determine why "it doesn't work." Error trapping is critical for this, a simple PHP example,
$link = mysql_connect(DBHOST,DBUSER,DBPASSWORD,true) or die ("Could not connect to remote server" . mysql_error());
In your case, I'd expect "permission denied for user@localhost" or something similar.