需要有关 php 中 mysql 连接的帮助

发布于 2024-09-15 06:43:06 字数 943 浏览 4 评论 0原文

我的 mysql 连接有问题。

我有一个上传照片的 php 脚本,该脚本执行以下操作:


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.

如果图像处理时间小于 5 秒(mysql 连接超时),则一切正常,否则执行步骤 4)时出现错误,错误为“Mysql 服务器已消失”。

因此,我检查了图像处理后mysql连接是否仍然存在,如果图像处理时间超过5秒,mysql连接就会终止。

我在图像处理(步骤 3)之后添加了此代码,


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

但它不起作用!这是我真正的问题。

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?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤单情人 2024-09-22 06:43:06

引用自 http://dev.mysql.com/doc/ refman/5.0/en/gone-away.html

MySQL服务器消失错误的最常见原因是服务器超时并关闭了连接。
[...]
MySQL 服务器消失错误的其他一些常见原因是:
* 您(或数据库管理员)已使用 KILL 语句或 mysqladmin Kill 命令终止了正在运行的线程。
* 您尝试在关闭与服务器的连接后运行查询。这表示应用程序中存在应更正的逻辑错误。
* 在不同主机上运行的客户端应用程序没有必要的权限从该主机连接到 MySQL 服务器。
* 客户端的 TCP/IP 连接超时。如果您使用以下命令,则可能会发生这种情况:mysql_options(..., MYSQL_OPT_READ_TIMEOUT,...) 或 mysql_options(..., MYSQL_OPT_WRITE_TIMEOUT,...)。在这种情况下,增加超时可能有助于解决问题。
* 您遇到了服务器端超时,并且客户端中的自动重连被禁用(MYSQL结构中的重连标志等于0)。
* 您使用的是 Windows 客户端,并且服务器在发出命令之前已断开连接(可能是因为 wait_timeout 已过期)。

ETC。

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.

夜夜流光相皎洁 2024-09-22 06:43:06

首先尝试增加超时,然后在第 4 步后关闭连接

mysql_close()

为什么要增加超时?因为图像处理肯定需要5秒以上,所以就会出现这个问题!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文