Jump to content

connection error in practice project


acallahan

Recommended Posts

I am working on a practice project for school, so I would prefer a response to my question that would help me learn what I am doing wrong as opposed to simply giving me the answer, but I am on a tight deadline for getting the project completed.

 

The error states that

Notice: Could not connect to MySQL: Access denied for user 'myuser'@'localhost' (using password: YES) in /home/acallaha/public_html/Corporanker/includes/connection.php on line 22

 

I have set up the database using a MySQL database wizard with the correct username and password, using localhost as the host, and making sure the database name was correct. I also made sure the user had all priviledges.

 

My connection.php script resembles what is below:

 

<?php

  $db_user = 'myuser';

  $db_password = 'mypassword';

  $db_host = 'localhost';

  $db_name = 'mydatabasename';

 

  $connection = @mysqli_connect($db_host, $db_user, $db_password, $db_name);

 

  if (!$connecton) {

    trigger_error ('Could not connect to MySQL: ' . mysqli_connect_error();

  }

?>

 

Can anyone see anything obvious as to why the user would be denied access? I can't start troubleshooting any other error until I get past the connection error!

Link to comment
Share on other sites

Notice: Could not connect to MySQL: Access denied for user 'myuser'@'localhost' (using password: YES) in /home/acallaha/public_html/Corporanker/includes/connection.php on line 22

 

It means that your database connection details are wrong. Is your username correct? Usually the default MYSQL username is "root"... assuming that you've just installed MySQL. The password on default is blank. Which means that it's $password = ""

Link to comment
Share on other sites

The textbook example uses mysqli.

 

We're using free hosting sites to display our projects, so I used the zendfree free hosting. I didn't need to install MySQL. I just needed to create the database and the table(s), set up the user, password, priviledges, etc.

 

I triple checked that all the spellings and cases matched up. They do.

Link to comment
Share on other sites

try this

 

<?php
  $db_user = 'myuser';
  $db_password = 'mypassword';
  $db_host = 'localhost';
  $db_name = 'mydatabasename';

  $connection = mysql_connect($db_host, $db_user, $db_password, $db_name);

  if (!$connecton) {
    trigger_error ('Could not connect to MySQL: ' . mysql_connect_error();
  }
?>

 

With all your details added back in

Link to comment
Share on other sites

I am working on a practice project for school, so I would prefer a response to my question that would help me learn what I am doing wrong as opposed to simply giving me the answer, but I am on a tight deadline for getting the project completed.

 

The error states that

Notice: Could not connect to MySQL: Access denied for user 'myuser'@'localhost' (using password: YES) in /home/acallaha/public_html/Corporanker/includes/connection.php on line 22

 

I have set up the database using a MySQL database wizard with the correct username and password, using localhost as the host, and making sure the database name was correct. I also made sure the user had all priviledges.

 

My connection.php script resembles what is below:

 

<?php

  $db_user = 'myuser';

  $db_password = 'mypassword';

  $db_host = 'localhost';

  $db_name = 'mydatabasename';

 

  $connection = @mysqli_connect($db_host, $db_user, $db_password, $db_name);

 

  if (!$connecton) {

    trigger_error ('Could not connect to MySQL: ' . mysqli_connect_error();

  }

?>

 

Can anyone see anything obvious as to why the user would be denied access? I can't start troubleshooting any other error until I get past the connection error!

 

much simpler version

 


<?php
$con = mysql_connect(localhost, "dbname here", "dbpass here");
$db = mysql_select_db(dbname here, $con);

if(!$con){
echo "ERROR: could not connect to database!\n";
}
?>

Link to comment
Share on other sites

Not to dis you ghostrider, just modifying your code to be more logical, cause if you are not connected, then there is no reason to select the DB. Also that code tells nothing about the error, IE why it would not connect.

 


<?php
$con = mysql_connect(localhost, "dbname here", "dbpass here");
if(!$con){
die( "ERROR: could not connect to database!<br /> With ERROR: " . mysql_error());
}

$db = mysql_select_db(dbname here, $con);


?>

 

So if no connection kill the script and tell them why it was killed. Good for development scenarios so you can see exactly what is going wrong, etc.

Link to comment
Share on other sites

The first suggestion gets me back to the original error message.

 

The second suggestion gives me this error:

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'acallaha'@'localhost' (using password: YES) in /home/acallaha/public_html/Corporanker/includes/connection.php on line 2

Database connection failed: Access denied for user 'acallaha'@'localhost' (using password: YES)

Link to comment
Share on other sites

remove the password, insert root in the dbusername bit and leave the dbpass bit empty so it should be

 

<?php
$con = mysql_connect(localhost, "root", "");
if(!$con){
die( "ERROR: could not connect to database!<br /> With ERROR: " . mysql_error());
}

$db = mysql_select_db(insert dbname here, $con);


?>

 

BTW premiso, you were right.

Link to comment
Share on other sites

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: NO) in /home/acallaha/public_html/Corporanker/includes/connection.php on line 2

Database connection failed: Access denied for user 'root'@'localhost' (using password: NO)

Link to comment
Share on other sites

are you sure you created a user in the database? root should work by default unless that was modified.

 

Check your database and make sure it allows for that username/password combo and or reset it so it matches what you want.

Link to comment
Share on other sites

Shared hosting is not going to allow access using the "root" user.

-----------------------------------------

 

Are you sure the mysql database host is localhost? There may in fact be a mysql server running on localhost, but I would bet that when you created your database/user/password that the host name or IP address displayed on the relevant screens was something like mysqlx.somedomain.com and not localhost.

 

Link to comment
Share on other sites

So what username/database name did you use in your other projects. The following is a extract from a Tutorial on their forum that indicates that you must add your cpanelusername as part of the user and database names -

 

The dbusername will be "cpanelusername_Dbusername" and the DB name will be "cpanelusername_dbname"
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.