3

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.

2
  • Are you sure that your mysql_select_db call actually always contains a database to select? Make a test output of $mysql to see whether there are instances when it is empty. Commented May 6, 2010 at 15:52
  • I added echo $mysql; and it gives me "Resource Id #3". I added some additional error handling and it gives me: SQL Error [256] (1046) No database selected Query : SELECT * FROM tbl LIMIT 1 On line 65, PHP 5.1.6 (Linux) Commented May 6, 2010 at 16:11

2 Answers 2

3

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 the mysql_pconnect/mysql_connect line.

Sign up to request clarification or add additional context in comments.

1 Comment

This appears to have worked. After editing both files exactly as you suggested, I tested selecting random names in the dropdown box over and over and every time, it's working with no errors. Am getting a couple other people to test and to make absolutely sure, but so far I'm confident in your fix. Thanks so much Rob! (BTW, cool last name too.)
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.