Link to home
Start Free TrialLog in
Avatar of Colin Brazier
Colin BrazierFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Mysql timeout on my xampp install

Hi experts,

I have had to reinstall XAMPP and am now getting this error after a couple of seconds...

Warning: mysql_connect(): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

My wait_timeout is the default 28800 and I have set global connect_timeout = 60, but still no difference.

Thanks for your help.

   Col

ps I know I shouldn't be using mysql_connect.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Sounds like your MySQL server has gone away. Are you able to connect to it with a query tool (SQLYog, Heidi, Workbench, PHPMyAdmin, Console interface)?
Avatar of Colin Brazier

ASKER

Oh yes Julian it takes me a few refreshes but the page appears eventually.
So, if I understand you correctly - it does sometimes connect?
Yes it always connects in the end (so far).
Do you experience the same delays when executing queries directly through anyone of the tools posted earlier?
No.  Workbench and mysql Maestro queries work without a problem.

It isn't the particular web page that's the issue, other pages that have worked fine in the past are now struggling on occasion (I hit 10 different pages and on two occasions I got that error).

Could it be my PC is under stress?  I have SQL Server running on it, I could stop some of its services and see if that helps.
Nope, that didn't help!
I doubt it is PC load.

To summarise,

Connection to the MySQL service using query tools - no issue
Web Page - connects - but intermittently gives a connection error.
Exactly.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
mysql, but this has never been an issue previously.
Not that that should be the cause of the problem - that library is deprecated and you should consider moving off it anyway.

Does this only happen with this specific page?
If you set up a script that just does the connect like so
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

Open in new window

And repeatedly call it - do you also get timeouts?
Yeah I know it's deprecated but it's a big job!
It isn't the particular web page that's the issue, other pages that have worked fine in the past are now struggling on occasion (I hit 10 different pages and on two occasions I got that error).

Yup, get the same intermittent problem with that connection script.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No, I haven't broken it once yet...
Looks like I'm going to have to bite the bullet.

Any reason you can think of why that's the cause?
No - but as that library has been deprecated for some time finding out why at this stage might be fruitless.

You could test against older versions of PHP to see if the problem goes away - but personally I would take the time to upgrade to mysqli.

I use an intermediary library for doing database calls - this obfuscates the underlying implementation which means if I change the db library the code still works.
I agree.  Could you elaborate on how your intermediary library works please?
It is implemented as a class.

In the constructor I create the internal mysqli object using config info.

I have a number of methods on the class that use that connection to do what they need to do - for instance

$db = new Database();
$db->find_first('#__tablename', $options);

Open in new window


I borrowed the table name convention from one of the CMS libraries - the find_first function first prepares the query using the $source and the $options.
$options can be a string (the filter) or it is an array with any combination of the following
filter => Where clause
sort => Order by clause
start => Limit clause (start)
total =>  Limit clause (total)

These are combined to create the final query.
The #__ is replaced in the source with a table prefix that is defined in the config.

find_first calls the query method with the constructed SQL and receives an array back.
It returns the first element in the array or false if none found

$db->find('#__tablename',$options);

Open in new window

Is similar but always returns an array or false - returns all rows that match the criteria.

I then have methods for
update_record
add_record
delete_record
And a few others for doing various things.

the add and update functions take associative arrays as input and build the INSERT field list and values dynamically.

Update and Add also do all sterilization of data being sent to the db.

I have a db library with the above for MySQL, MySQLi and PDO - which I can use interchangeably with the main code.
Wow, I have a way to go before my procedural brain can get to that, but it's definitely something to aim for.

Thanks for your help, much appreciated.
You are most welcome.