Get this in your error logs from time to time and want to find out the problem:
[04-Oct-2014 13:25:42 America / Chicago] Could not connect to the database: SQLSTATE [HY000] [2002] Connection timeout [04-Oct-2014 13:25:42 America / Chicago] PHP Fatal error : call the prepare () member function on a non-object in / home / spconlin / public _html / spc_app / app / add_log.php on line 28
Connect script:
<?php if(!defined('INCLUDE_CHECK')) die('You are not allowed to execute this file directly'); // db config $db_host = '127.0.0.1'; $db_database = '*****'; $db_user = '*****'; $db_pass = '*****'; //db connection try { $db = new PDO("mysql:host=$db_host;dbname=$db_database;charset=utf8", $db_user, $db_pass, array( PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //PDO::ERRMODE_SILENT is default setting PDO::ATTR_PERSISTENT => false ) ); } catch(PDOException $e) { error_log("Failed to connect to database: ".$e->getMessage()); } ?>
What I've done:
- switch from 'localhost' to 127.0.0.1 in the connection
- Switch persistent connections to false
Before making the above changes to my connection, I got SQLSTATE[HY000] [2002] Resource temporarily unavailable , not connection timed out , as shown above.
Some background information is the add_log.php script, in particular, it adds information to the database up to several times per second when it is actually used. While almost 99% of the time, this particular script refers to an error, sometimes others. I understand that the script does not really mean anything, because this is not the place where the error occurs (this happens in the connection, and not in the add_log.PHP script file), but it is strange that it is almost always this particular file that leads me to the idea that the fact that he is called so often in a row is part of the problem.
DB is innodb.
Is this really a problem with my server and db or the person / computer connected to it? Is it possible that βtheyβ have a bad connection with βme,β and this is the cause of the error?
EDIT:
Could it be the way I need my script connection on the pages you need? Since the file βhitβ quite often, it will also require my script connection in the same way as in the line below. Please note that I am using $_SERVER['DOCUMENT_ROOT'] (can this affect?).
$ _SERVER ['DOCUMENT_ROOT'] required. '/custom/functions/connect.php';