need help with mysql connection in php

277 Views Asked by At

I have a problem with mysql connection.

I have a php script which upload photo, this script do that:


1) mysql connection
2) check user rights for uploading photo (I use a sql query for this)
3) Image processing
4) sql query for insering photo data in database
I have set a mysql connection timeout to 5 s.

Everything works great if the image processing time is less than 5s (mysql connection timeout) otherwise I got a error when the step 4) is executed, the error is "Mysql server has gone away".

So, I checked if the mysql connection was still alive after the image processing and the mysql connection died if the image processing time is more then 5s.

I added this code after the image processing (step 3)


if(!mysql_ping($conn)){
$conn = mysql_connect('localhost', 'mysqluser', 'mypass');
$db   = mysql_select_db('mydb',$conn);
}

but it doesn't work! this is my real problem.

var_dump($conn)
give a mysql ressource link but

mysql_select_db('mydb',$conn)
return FALSE, and
mysql_error()
return "mysql server has gone away"

Does anybody can help me?

Thanks in advance.

2

There are 2 best solutions below

1
On BEST ANSWER

Quote from http://dev.mysql.com/doc/refman/5.0/en/gone-away.html:

The most common reason for the MySQL server has gone away error is that the server timed out and closed the connection.
[...]
Some other common reasons for the MySQL server has gone away error are:
* You (or the db administrator) has killed the running thread with a KILL statement or a mysqladmin kill command.
* You tried to run a query after closing the connection to the server. This indicates a logic error in the application that should be corrected.
* A client application running on a different host does not have the necessary privileges to connect to the MySQL server from that host.
* You got a timeout from the TCP/IP connection on the client side. This may happen if you have been using the commands: mysql_options(..., MYSQL_OPT_READ_TIMEOUT,...) or mysql_options(..., MYSQL_OPT_WRITE_TIMEOUT,...). In this case increasing the timeout may help solve the problem.
* You have encountered a timeout on the server side and the automatic reconnection in the client is disabled (the reconnect flag in the MYSQL structure is equal to 0).
* You are using a Windows client and the server had dropped the connection (probably because wait_timeout expired) before the command was issued.

etc.

1
On

first try increasing the timeout, then close the connection after step 4

mysql_close()

why increase the timeout? because the image processing definitely takes more than 5 secs, so this problem occurs!