Connecting to a database...

sathanna

Member
Prime Account
Messages
63
Reaction score
0
Points
6
So, I've tried connecting to a database I made... but I can't get the servername right. Apparently, after a little search of the forums, I am not allowed to connect to x10 or whatever to get to my database. I'm confused.

I've pretty much copy pasted some code I found somewhere to help me connect to the database... looks something like this:

Code:
// hostname or ip of server
$servername='localhost';

// username and password to log onto db server
$dbusername='myusername';
$dbpassword='mypassword';

// name of database
$dbname='my_database';

////////////// Do not  edit below/////////

connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());

So is it a hopeless cause and I can't use my database at all, or is it something to do with the code? I'm new to php and mysql and whatnot, so I really have no clue.

Thanks.
 

Zubair

Community Leader
Community Support
Messages
8,766
Reaction score
305
Points
83
make sure you are using correct username and password..

The correct sytax of username and database is

DB Name = satha_[dbname]
USER = satha_[USER]
 

sathanna

Member
Prime Account
Messages
63
Reaction score
0
Points
6
I'm pretty sure I got that part right. I double checked them and everything. I just edited that out for showing it to people... I don't need the brackets though right?

what am I meant to put in the servername part so I can connect to the database? =s
 

Zubair

Community Leader
Community Support
Messages
8,766
Reaction score
305
Points
83
I'm pretty sure I got that part right. I double checked them and everything. I just edited that out for showing it to people... I don't need the brackets though right?

what am I meant to put in the servername part so I can connect to the database? =s

Yeah you don`t need to use brackets. replace [dbname] with database name.
In servername you must enter localhost
Which is shown correct in the above code.

Did you add user to database in cpanel??
 

sathanna

Member
Prime Account
Messages
63
Reaction score
0
Points
6
dang.. I tried localhost. I'm sure everything else is okay. Perhaps everything IS okay...it's just another part that's freaking out.

After a check, I think it might just be that I am not creating the table correctly... baahhh.. so confusing. :eek4: At least I'm reassured that localhost IS the right thing to put there.

Thank you. Time to go look up more stuff on creating data and whatnot. :happysad:
 
Top