How to connect my php script to database?

flash.engine50

New Member
Messages
7
Reaction score
0
Points
0
Hi, I am here new and I don't know what I have to do. I have read somethings in forum, but I didn't find.
I have created database, mysql user, added user to database and I gave all permission to user.

my php script:

$host="localhost"; // Host name
$username="hawksvk_fe"; // Mysql username
$password="mypassword"; // Mysql password
$db_name="hawksvk_database"; // Database name

//Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

Result: Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'hawksvk_fe'@'web7.vital.x10hosting.com' (using password: YES) in /home/hawksvk/public_html/aaa.php on line 11

Please help me, thank you.
 

niagaradad

New Member
Messages
9
Reaction score
0
Points
0
you are passing in a password, are you sure it is the correct password? If the password wasn't being accepted it would say (using password: No).
 
Last edited:

MaestroFX1

Community Advocate
Community Support
Messages
1,577
Reaction score
60
Points
0
Post the full script or at least post what's around line 11.

Because the lines that you have posted alone can't do that.

There would be only two outcomes in this case(error)
#1 cannot connect to server
#2 cannot select DB
 

flash.engine50

New Member
Messages
7
Reaction score
0
Points
0
mysql_connect("$host", "$username", "$password")or die("cannot connect to server"); //this is line 11, password is good
 

MaestroFX1

Community Advocate
Community Support
Messages
1,577
Reaction score
60
Points
0
Just now I (re)generated the error on my and clearly arrived at conclusion that it is an “authentication error”.

Follow this step-by-step:

1. Go to your cPanel page and click “MySQL Database Wizard”, you will be asked to create a database (mysql).
Type a name for your database,for example – mydb001.

$db_name = yourusername_whatyoutypedjustnow.

For you, $dbname = hawksvk_mydb001 //to be used in php

Click Next.

2. Enter username and password “for the database” (hawksvk_mydb001).
Characters for username <= 7.
$username = hawksvk_usr001 //to be used in php

3. Temporarily grant all privileges and proceed.

4. You will get conformation that
User hawksvk_usr001 was added to the database hawksvk_mydb001.

Check this page “MySQL Databases” before you go any further to ascertain that the created database and user are linked with each other.
If not, use “Add User To Database“and link them.

Now, php section:

------start of code

<?
$host="localhost"; // Host name
$username=" hawksvk_usr001"; // Mysql username
$password="mypassword"; // Mysql password
$db_name=" hawksvk_mydb001"; // Database name

//Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

?>
------end of code
 

flash.engine50

New Member
Messages
7
Reaction score
0
Points
0
I have made all. Now it is this same. :(

Warning: mysql_connect() [function.mysql-connect]: Access denied for user ' hawksvk_usr001'@'web7.vital.x10hosting.com' (using password: YES) in /home/hawksvk/public_html/aaa.php on line 8
 

MaestroFX1

Community Advocate
Community Support
Messages
1,577
Reaction score
60
Points
0
There shouldn't be any spaces between the starting " and the last last " for a variable.
Try this:
$host="localhost";
$username="hawksvk_usr001";
$password="mypassword";
$db_name="hawksvk_mydb001";

Remove spaces!

---------- Post added at 04:36 PM ---------- Previous post was at 04:34 PM ----------

And I see "a space" in your error page.


.............user ' hawksvk_usr001'@.............

It should be 'hawksvk_usr001'@ not ' hawksvk_usr001'@
 

callumacrae

not alex mac
Community Support
Messages
5,257
Reaction score
97
Points
48
Don't use mysql_*. It is slow, out of date, and doesn't allow you to use the newest features of MySQL.

Check out PDO, you can find a tutorial here

~Callum
 

MaestroFX1

Community Advocate
Community Support
Messages
1,577
Reaction score
60
Points
0
Bumper !

As a last resort try abcd123 or something like that as your database password.

Something is going wrong during the connect stage.
 

flash.engine50

New Member
Messages
7
Reaction score
0
Points
0
// Database connection info
$host = 'localhost';
$port = 3306;
$database = 'hawksvk_mydb001';
$username = 'hawksvk_usr001';
$password = 'mypassword';
// Construct the DSN
$dsn = "mysql:host=$host;port=$port;dbname=$database";
// Create the connection
$db = new PDO($dsn, $username, $password);

still error :(
 

MaestroFX1

Community Advocate
Community Support
Messages
1,577
Reaction score
60
Points
0
// Database connection info
$host = 'localhost';
$port = 3306;
$database = 'hawksvk_mydb001';
$username = 'hawksvk_usr001';
$password = 'mypassword';
// Construct the DSN
$dsn = "mysql:host=$host;port=$port;dbname=$database";
// Create the connection
$db = new PDO($dsn, $username, $password);

still error :(

Yet again authentication error - SQLSTATE[28000] [1045]


Try this:

<?
$host="localhost"; // Host name
$username="hawksvk_usr001"; // Mysql username
$password="abcd123"; // Mysql password
$database="hawksvk_mydb001"; // Database name


$dsn = "mysql:host=$host;dbname=$database" ;

try {
$dbh = new PDO($dsn, $username, $password);

echo "<p>Finally Successful</p>";
$dbh = null;
}
catch (PDOException $e) {
die($e->getMessage());
}
?>
---------------
Use encoding : us-ascii

Check for ticks.
 
Top