间歇性“未选择数据库”在 PHP/MySQL 中?
有一个 PHP/MySQL 表单,其中包含一个包含 350 个名称的列表的下拉框。当选择任何随机名称时,有时它会起作用并且可以正常工作。显示数据库中有关该名称的信息,有时该表单会给出错误“未选择数据库”。
这是我尝试过的,因为我不是程序员,所以几乎抓住了稻草:将
- /etc/my.cnf 中的 max_connections 从 200 增加到 2000 (即使只建立了 4-5 个连接,并且它是一个很少使用的服务器)
- 将 mysql_pconnect 更改为 mysql_connect
将单词 true 添加到此连接字符串:
$mysql = mysql_pconnect($hostname_mysql, $username_mysql, $password_mysql, true) 或 trigger_error(mysql_error(),E_USER_ERROR);
将此行中的单词 require_once 更改为 require:
[?php require('/home/user/Connections/mysql.php'); ?]
启用 MySQL 和PHP查询&错误记录。 (没有记录错误)
这里是代码:[删除了旧的错误代码]
更新:下面 Rob Apodaca 的工作答案。
Have a PHP/MySQL form with a dropdown box containing a list of 350 names. When any random name is selected, sometimes it works & displays info about that name from the database, and sometimes the form gives the error "No Database Selected".
Here's what I've tried, pretty much grasping at straws as I'm not a programmer:
- Increasing max_connections in /etc/my.cnf from 200 to 2000 (even though only 4-5 connections are made and it's a lightly used server)
- Changing mysql_pconnect to mysql_connect
Adding the word true to this connection string:
$mysql = mysql_pconnect($hostname_mysql, $username_mysql, $password_mysql, true) or trigger_error(mysql_error(),E_USER_ERROR);
Changing the word require_once to require on this line:
[?php require('/home/user/Connections/mysql.php'); ?]
Enabling MySQL & PHP query & error logging. (no errors logged)
Here is the code: [removed old bad code]
Update: Working answer from Rob Apodaca below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此行:
mysql_select_db($database_mysql, $mysql);
在您提供的脚本中出现多次。由于 $database_mysql 的值似乎没有改变,我将删除该行的所有实例并将其放置在 mysql_pconnect/mysql_connect 行之后。This line:
mysql_select_db($database_mysql, $mysql);
appears more than once in your provided script. Since the value of$database_mysql
does not appear to change, I would remove all of the instances of that line and place it just after themysql_pconnect/mysql_connect line
.我的问题是在另一个序列化对象 (A) 中序列化的数据库连接对象 (B)。有时,根据对象 A 反序列化之前发生的情况,对象 B 会使用空数据库名称执行 mysql_select_db() 调用,因为它已丢失。
我们在代码中散布了一些 MySQL 查询“
SELECT DATABASE()
”结果的回显,直到找到默认数据库间歇性丢失的位置。希望这有帮助!My issue was with a database connection object (B) that was serialized within another serialized object (A). Sometimes, depending on what had happened prior to object A being unserialized, object B would perform a
mysql_select_db()
call with a null database name as it had been lost.We littered our code with some echo's of the result of a MySQL query "
SELECT DATABASE()
" until we found where the intermittent loss of the default database was occurring. Hope this helps!