...

View Full Version : php mysql_connect



DHTML Kitchen
03-13-2004, 04:53 PM
Client has 2 username/pws.

I can connect via mysql monitor just fine. I can connect via mysql_connect in PHP. I cannot use the db with the link/connection returned from mysql_connect for mysql_select_db. It's weird, see:

username 1 - used for ftp
username 2 - used for mysql monitor and mysql_connect

pass 1 - ftp password, works for mysql_connect pass
pass 2 - mysql monitor pass


I can get a mysql_connect connection (link), but I can't connect to the db with mysql_select_db. I think mysql_select_db fails because it requires pass 2, not pass 1, but mysql_connect requires pass 1.

What is wrong?

Nightfire
03-13-2004, 07:20 PM
Can you show your code? I'm confused to what you're saying

raf
03-13-2004, 07:56 PM
Originally posted by DHTML Kitchen
What is wrong?
Probably your code.

Your connectonstrings should look like


$link = @mysql_pconnect("localhost", "yourusername", "yourpassword")
or die("Could not connect to the databaseserver. Please go back and try again or try again later.");
@mysql_select_db("yourdbname",$link)
or die ("Could not select database. Please go back and try again or try again later.");

You need to replace the italis by tour user, pwd and dbname.
If you connect from a remote server, the localhost needs to be replaced by the mysql-servers IP or name, but you'll probaly connect from the same machine.

DHTML Kitchen
03-13-2004, 09:41 PM
$link = mysql_connect('localhost', 'ehousebo', '--deleted-pw--');
if (!$link) {
die('Not connected : ' . mysql_error());
}

// make ehousebo the current db
$db_selected = mysql_select_db('w3webde_launch', $link);
if (!$db_selected) {
die ('Can\'t use w3webde_launch : ' . mysql_error());
}



output:

Can't use w3webde_launch : Access denied for user: 'ehousebo@localhost' to database 'w3webde_launch'

http://www.ehouseboatsite.com/brochure/

DHTML Kitchen
03-13-2004, 10:14 PM
mutherfudge!

I just reallized that you used a pconnect function. Well, that WFM. Didn't see that in the manual but I'm off to take a nother look at it.


Thanks bro.

raf
03-13-2004, 11:10 PM
you can use mysql_connect --> probably better for your app.

You should get an error on
'Can't use
(just look at the colourcoding and you'll see it should hae been either
"Can't use...:"
or
'Can\\'t use...:'

as you could have seen where you copied it
http://www.php.net/function.mysql-select-db

Although this is code from the manual, it's not good. It is unefficent and not safe since it discloses to much info about your db account and the db-name. The most effient code would be


$link = @mysql_connect('localhost', 'yourusername', 'yourpassword')
or die('Could not connect to the databaseserver. Please go back and try again or try again later.');
@mysql_select_db('yourdbname',$link)
or die ('Could not select database. Please go back and try again or try again later.');

--> using mysql_connect (which in most case is to be prefered over pconnect) and single quotes (which is supposed to be slightly faster since the parser then doesn't expect enclosed variables)

<edit> the backslash in the second alternative got parsedaway</edit>

DHTML Kitchen
03-15-2004, 01:16 AM
good advice. Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum