Use mysql_real_escape_string always when using them in some of your queries. As bradgrafelman pointed out, you're not using the sprintf function correctly. This is the right way to way to use it:
$checklogged = sprintf("SELECT session.id FROM session, user WHERE user.username = '%s' AND session.id = user.id",
$_SESSION['username']);
In sql inserts this comes very handy. Heres an example:
$sql = sprintf("INSERT INTO tablename (name,email,homepage) VALUES ('%s','%s','%s')",
mysql_real_escape_string($name),
mysql_real_escape_string($email),
mysql_real_escape_string($homepage));
Its lot cleaner than the usual way:
$sql = "INSERT INTO tablename (name,email,homepage) VALUES ('" . mysql_real_escape_string($name) . "','" . mysql_real_escape_string($email) . "','" . mysql_real_escape_string($homepage) . "')";
Generally you use %s for strings and %d for integer values. Check out the manual page for sprintf function for more info.
About the sql injection attack by using $_SESSION['username'].. Well you should save the user id to the session instead of username and then use the sprintf with %d. That way if the user somehow gets to manipulate session variable(very unlikely though) it would not help the attacker because the query would only accept integer values.
Normal users dont need to know what mysql error the site gives so its better to show them something like "Database error" etc. Attackers can learn from the errors (field names etc.) and try to find a security hole in your site. Less information is better.
Save the errors to some log and maybe send email to yourself to acknowledge that there is an error.