Jump to content

'Table doesn't exist' error because 'mysql_select_db' only works once?


StevieCritoph

Recommended Posts

Hi,

 

My site was working fine, but I just switched servers/web hosts and now I'm getting the following errors (in blue). The thing is, I don't get the error if I try to run the query loop after just using 'mysql_select_db' once, but if I use it again to select a different database and then run the query loop, I start getting the error.

 

mysql_select_db ("database1", $mysql_con);

$mysql_query = mysql_query ("SELECT ID FROM someTable1", $mysql_con);
$someVar1 = 0;

while ($mysql_array = mysql_fetch_array ($mysql_query))
{
$someVar1 ++;
}


////// The above code gives me no error. But If I then try to select a different database and run a loop in the same way (in the same file), I start getting the below error...


mysql_select_db ("database2", $mysql_con);

$mysql_query = mysql_query ("SELECT ID FROM someTable2", $mysql_con);
$someVar2 = 0;

while ($mysql_array = mysql_fetch_array ($mysql_query))
{
$someVar2 ++;
}

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/snubby5/public_html/coozymcmillan.com/view.php on line 86

 

So I looked it up and somewhere it told me to add some extra code to handle errors better:

 

if ($mysql_query === FALSE) {
    die (mysql_error ()); // TODO: better error handling
}

 

But when I do that, I get a different error:

 

Table 'database1.someTable2' doesn't exist

 

It doesn't seem to recognize that I switched databases, and it says 'database1.someTable2' doesn't exist (because it doesn't), when really it should be checking for 'database2.someTable2' (which exists).

 

So to reiterate, I can run the query loop fine after selecting database1, but after that if I select database2, it either gives me the first error, or If I add the 'die' code it gives me the second error.

 

 

Thanks much!

 

PS: I can get it working if I put everything into one database, but I have multiple websites (which sometimes need to call eachothers' databases) and it would be a huge hassle to cram them all into one database unless I have to.

Link to comment
Share on other sites

Debug why the second mysql_select_db statement is failing by changing it to the following -

 

mysql_select_db ("database2", $mysql_con) or die("Select db database2 failed due to: " . mysql_error($mysql_con));

 

Thank you so much, it works now. Turns out I forgot to add that user to all the databases, I just added it to the first one. I'll be sure to use 'die' to help debug things from now on.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.