$conn = mysql_connect('localhost', 'root', 'xyvz5j');
mysql_select_db('rata2', $conn);
How can I do so this so that it won't print a lot of garbage like "Failed to connect" or something (if there's a MySQL error)?
One person's "garbage" in another's "informative error message" :)
I would recommend not suppressing them with @ but instead have your production server configured to not display error messages at all. You can have them go to a file for periodic review. However, your staging/development systems should be set to output everything, errors, warnings, notices, the lot.
See the error_reporting configuration directive for details.
Put an at symbol in front of the calls.
$conn = @mysql_connect('localhost', 'root', 'xyvz5j');
@mysql_select_db('rata2', $conn);
mysql_connect issues an warning when there are connection problems.mysql_connect does report a warning when it fails.Just check what value mysql_connect returned before using it:
$conn = mysql_connect('localhost', 'root', 'xyvz5j');
if (!is_resource($conn)) {
// error
} else {
mysql_select_db('rata2', $conn);
}
But the warning for a failed connection for mysql_connect cannot be avoided in this way. But you can disable display_errors so that the error is not shown.
You could set an error_reporting level of 0. Mandatory in production code, ugly when developing. So have separate values for the two environments.