How to connect to mysql with php

software development

#1

Hi, I am looking for advice as to what to use for the server name(I used localhost) parameter when trying to connect to a mysql database. I am using this but getting an error. Any ideas?

$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
die('Not connected : ’ . mysql_error());


#2

When you create the database using the DreamHost control panel you will see that you are given the choice to create your own Hostname or accept the default of mysql.example.com.

You must use this hostname. You cannot use ‘localhost’ with DreamHost.

So in your scripts wherever you see ‘localhost’ use the correct mysql hostname.

See here


Norm

Opinions are my own views, not DreamHosts’.
I am NOT a DreamHost employee OK! :@

Act on my advice at your own risk!


#3

Getting acces denied, so I must have more than that wrong…ugh

Thanks for the advice! I was wondering what that 2nd chance at making a hostname was all about…

Is this nitti stuff correct when trying to connect?

Not connected : Access denied for user ‘sid’@‘nitti.dreamhost.com’ (using password: YES


#4

Are you running a php script that is trying to access the MySQL database locally on your account or are you trying to access it remotely say from home/work?

Check your script uses your MySQL hostname,user and password and that they have been entered correctly.


Norm

Opinions are my own views, not DreamHosts’.
I am NOT a DreamHost employee OK! :@

Act on my advice at your own risk!


#5

I have the php file on the server and I’m just trying to hit the file in IE.

I made the database earlier and I have connected through the web based phpadmin. I’ve checked over the user, password and added your advice to the hostname. I’m kinda stumped now…

I got this code and it looks reasonable…I have a require statement which holds these variables.

// Opens a connection to a mySQL server
$connection=mysql_connect ($hostname, $username, $password);
if (!$connection) {
die('Not connected : ’ . mysql_error());
}

// Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can’t use db : ’ . mysql_error());
}


#6

Maybe I can’t even connect this way.

I have a php file called output.php which required dbinfo.php to connect.

Its getting to the connection and saying access is denied. I’m just hitting the url in IE.

www.myurl.com/output.php

Is that doable?


#7

I think I might be connected now. My problem was that I had double quotes around all my variables of username, password, db name and hostname…


#8

Thanks for your forum etiquette, it’s nice to see. Does everything seem to be resolved now? If not please post back.