问题
Let's say I do a odbc_connect call, deliberately using erroneous information, as follows:
<?PHP odbc_connect('bogus','bogus','bogus'); ?>
Now, the manual states that odbc_connect "[r]eturns an ODBC connection id or 0 (FALSE) on error". I'm okay with the 0 being returned, but when I'm running the file (using Wampserver), I also receive error messages telling me that something went wrong.
I would like to suppress this error message, since I'm trying to build a PHP file that only echoes a certain piece of text, like "unsuccessful", when the information for the database call was wrong.
回答1:
You can use the error-suppression operator @
.
<?php
$conn = @odbc_connect('bogus','bogus','bogus');
?>
回答2:
Use a try-catch
:
<?php
try {
odbc_connect('bogus', 'bogus', 'bogus');
} catch (Exception $e) {
// handle your exception
}
回答3:
You can also use @
to suppress error messages on a single line - but it isn't good practice.
<?PHP @odbc_connect('bogus','bogus','bogus'); ?>
Error messages are there for a reason, don't ignore them. Use something like @Matt suggests and trap them as needed - not just hush them up.
来源:https://stackoverflow.com/questions/12074937/suppress-php-error-from-a-odbc-connect-call