Prevent Error on mysql_connect() - Access denied
Asked Answered
T

4

6

I have this code snippet:

$conn = mysql_connect($host, $usr, $pwd);

How can I prevent PHP from printing an error message when MySQL access is denied?

I need exactly this syntax but nothing works for me.

I tried $conn = mysql_connect($host, $usr, $pwd) or false; and $conn = mysql_connect($host, $usr, $pwd) || false; and the following:

try {
    $conn = mysql_connect($host, $usr, $pwd);

    if(!$conn) {
        throw new Exception('Failed');
    }
} catch(Exception $e) {
    // ...
}

PHP always prints an error message that the connection failed. But I just want to return false, if it failed.

Trudietrudnak answered 20/9, 2011 at 12:39 Comment(0)
S
6

I don't know why you want to, but you can add a @ to surpress php errors?

see: http://php.net/manual/en/language.operators.errorcontrol.php

Seminar answered 20/9, 2011 at 12:41 Comment(1)
Thanks, that's it! :) Didn't know that before.Trudietrudnak
L
10

Error printing should not be activated on a production server. Consider changing your php.ini configuration to log error instead of displaying it.

Using @ to silent error message is not a good solution.

Locative answered 20/9, 2011 at 12:44 Comment(1)
+1: People need to stop spattering @ everywhere. Configure your error levels properly instead.Horsy
S
6

I don't know why you want to, but you can add a @ to surpress php errors?

see: http://php.net/manual/en/language.operators.errorcontrol.php

Seminar answered 20/9, 2011 at 12:41 Comment(1)
Thanks, that's it! :) Didn't know that before.Trudietrudnak
M
3

You shouldn't really be using mysql_connect in PHP 5, as its now deprecated in favour of mysqli and PDO.

But you can probably silence the warning generated by mysql_connect by prefixing with @:

@mysql_connect()

Mercedes answered 20/9, 2011 at 12:42 Comment(0)
A
0

The @ suppresses errors. And also set error reporting error_reporting(0); to turn off all errors. Also check out mysqli

Docs

Andreeandrei answered 20/9, 2011 at 12:46 Comment(1)
Don't turn off your error reporting. Instead you should configure display_errors and log_errors in your php.ini.Chinchy

© 2022 - 2024 — McMap. All rights reserved.