PDA

View Full Version : Strange DB Selection Problem



Mike
02-11-2004, 11:17 AM
Hi all,

I have come across a strange database selection problem (unless I've missed something). My code is as follows:



<?php
//connect to db
$connect = mysql_connect("localhost", "user", "pass");

//select db
$db = mysql_select_db("db", $connect);
if (!$db) {
echo "db dont work";
}


"db dont work" is printed when I include that in one of my scripts. So I guess it can't select the database.

But why? The db name is correct, the username and password are, so why doesn't it select the database ever?

Anyone know whats wrong?

Thanks,
Mike

Chris
02-11-2004, 11:33 AM
some hosts prefix database names with account names like

accountname_dbname

So when you name your db "dbname" it is really "accountname_dbname"

This might be the case here.

chromate
02-11-2004, 11:42 AM
Yeah. Mine does that. Are you using cpanel Mike? Check the name in there.

MarkB
02-11-2004, 12:08 PM
The db username would be the same (ie, accountname_dbuser).

Mike
02-11-2004, 12:11 PM
Yeah, it's the same on my host. It was in my file as account_dbname I'm afraid:(

Anymore suggestions please?

Thanks a lot,
Mike

Chris
02-11-2004, 12:36 PM
Maybe your error checking simply is not working. Try running a query and see what happens.

Mike
02-11-2004, 12:39 PM
Yeh, first I ran a query, it came up with the following error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/dd/public_html/index.php on line 192

So I tried to check for an error. I'm beginning to think that MySQL is just not working, but it's working on my other sites which are hosted on the same server.

Anyone got anymore ideas? :)

Thanks,
Mike

Mike
02-11-2004, 01:57 PM
Right, well I seem to have got this solved, sort of ;)

I tried the script on another site, using a different database and it worked. I tried it on a site that I haven't used a database on before and it didn't. So it seems like my host may have to accept databases before they will work. Or you have to confirm that you will be using MySQL.

I dunno, but it's nothing to do with the code.

Thanks for the input anyway guys:)

eMEraLdwPn
02-11-2004, 09:29 PM
did you add the username to access the database? i think i had that problem when i was first getting set up with cpanel, i forgot to give myself access to the db :P

Mike
02-12-2004, 12:38 AM
OMG! Your right :)

I just made the username but didn't add it, thanks mate:)

Mike
02-12-2004, 01:06 PM
Please delete, I posted in the wrong thread :)