SQL newbie here. How do i create a connection from php?

timur

New Member
Messages
38
Reaction score
0
Points
0
Hi everyone, I am having problems with accessing the database that i have created.

In php i put in the following code and im not sure what goes in local host? is it localhost, or is it an address of the sql database? or my x10 website?

mysql_connect(localhost,timurtimur,$password);

Here is the error message:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'timurtimur'@'localhost' (using password: YES) in /home/timur/public_html/tim.php on line 27

Could not connect:
Access denied for user 'timurtimur'@'localhost' (using password: YES)


Appreicate any help ,

Thanks,

Timur
 

LHVWB

New Member
Messages
1,308
Reaction score
0
Points
0
You are correct in thinking that you need to set the host to localhost.

Check that you have created a user for your database in cpanel and allowed that user to the database (important, and is not automatic!), also check that the username and the password are correct and the same.

The error that you have is a failed login warning, so check those settings.
 

timur

New Member
Messages
38
Reaction score
0
Points
0
Aah yeah i forgot an underscore in the user name, thanks
Edit:
PHP:
<?php
 $password = "***********";
 $name  = "timur_user1";

 mysql_connect(localhost,$name,$password);
if (!$con)
{
die('<br />Could not connect: <br />' . mysql_error());
}
 echo "blah connected blah, disconnecting"
 mysql_close($con);
   echo "disconnected";
?>

what happens is that i get this

Could not connect:


and thats it..

So i can connect now that i have put in the correct username/pw, but now the could not connect message comes up with nothing after it? is there a reason for it not connecting?

 
Last edited:

Hazirak

New Member
Messages
197
Reaction score
0
Points
0
Might just be me, my eyes tend to get a little wacky when it's this late...

... but it looks to me like you misspelled "localhost"... :)
 

timur

New Member
Messages
38
Reaction score
0
Points
0
oh yeah that was me retyping in the forum i still have the problem
 

LHVWB

New Member
Messages
1,308
Reaction score
0
Points
0
Try:
PHP:
mysql_connect('localhost',$name,$password);


you probably need the '' around localhost:
 

federico_casares

New Member
Messages
49
Reaction score
0
Points
0
Anyway, it's always a good idea to use PDO instead of native mysql functions. It's faster and more secure. It's also easier to change databases in the future... Just my 0.02

www.php.net/pdo

It looks a bit tricky at first, but once you start to learn how to work with it, you'll love it. And you'll have faster and more secure scripts! :)
 
Last edited:

timur

New Member
Messages
38
Reaction score
0
Points
0
Im just a beginner right now, I think I should learn a bit more sql before going on to PDO, i will definitely look into it when i feel more confident with SQL.

Thanks for the insight,

Timur
 
Last edited:
Top