Cannot turn off errors from @mysql_connect – PHP



Hi All,

I have a script that runs on two servers – one with SQL, and one without.
I have coded it so that it doesn’t matter on which server it runs, but when I try to connect (to determine which strategy to follow) with:

function connect_to_sql_ok() {
$conn = @mysql_connect($this->mysql_host, $this->mysql_user, $this->mysql_pass) or die('<hr />MySQL Error: ' .mysql_error(). '<hr />');
if ($conn != false) {
$rs = mysql_select_db($this->mysql_db, $conn);
return $conn;

.. I get "MySQL Error: Access denied for user blahblah@blah.
This is on the server without MySQL. The same code with access to MySQL runs just fine.

I don’t want the error getting printed out – I just want the routine to quietly return FALSE, so that I can get on with the other strategy.

I have set as follows:
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

I thought that @ would suppress errors from a function, but it seems not..

Does anyone have any idea?



Heya, AgentSpanky.

You can try turning off display_errors (PHP: Runtime Configuration – Manual), but this will kill error messages for all errors, and they will still show up in your log.

One idea would be to try checking for mysql’s pidfile before attempting to connect (Class: Daemons::PidFile). MySQL generally stores this in the var directory within the installation directory that was specified when MySQL was installed.

Checking for the pidfile will only work if you are running the database on the same machine as your web server.


Hi pbmods,

Agree that probably turning off all errors is not what I want..

I’m not sure if php offers a process ID hook – I can’t find one. I looked in the /var/ directory, and the /var/db/ also, but couldn’t see anything interesting.

I tried get_loaded_extensions() which "returns the names of all the modules compiled and loaded in the PHP interpreter".
That didn’t help much, as mysql has been enabled, but just doesn’t exist.

Any other ideas why this particular @ doesn’t work?



Take away the or die() clause at the end of your connection variable.


Hi Markus,

Yep – done that.

My problem isn’t how to display or handle the error, but rather that it occurs in the first place.
Any other way of detecting if there is an sql service available that doesn’t raise an error when it fails would be ok, too..



Well, I’ve given up.
I’m now just checking which server its on (with a global variable – yuk!) based on it’s URL, and determining behaviour based on that.

Thanks for your comments..


You could use a try-catch block.