Error accessing MySQL database

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Error accessing MySQL database

Postby NinjaGaid3n » 12. February 2009 03:16

Hi I am really new to using Xamp, and ahve encountered numerous problems with getting the software to work. I think i have everytihing finally fixed, except I have created a database in phpmyadmin, called users. and i have put the php files in the htdocs folder to load the files, but when I try to load the page i get this error message:

MySQL Error: Access denied for user ''@'localhost' to database 'users'

I really do not know what I have done wrong. I have changed the username and password in the WinMysql my ini.setup page. and have chanfged the password to the same on the security page within localhost/xamp to the same. I saw a previous thread with a similar problem. this one:

viewtopic.php?f=16&t=32710&p=132255&hilit=MySQL+Error%3A+Access+denied+for+user#p132255

But I do not understand how the problem was fixed. I would really appreciate it for some assistance, thanks alot
NinjaGaid3n
 
Posts: 3
Joined: 11. February 2009 16:59

Re: Error accessing MySQL database

Postby Izzy » 12. February 2009 03:27

Use phpMyAdmin.

The usual information a script requires is
Db host = localhost
Db name = users
Db user = name here
Db user password = pass here

In phpMyAdmin under privileges you need to add a user to your database but don't use the super user root, choose another name and then give that username a password and all privileges for the database users only.

Here is a link to a post that explains how to create a database for WordPress but is applicable to create a database for any script, just change the names to suit your needs.
viewtopic.php?p=112258#p112258

Please let us know back if the post was any help.
Thanks.
Izzy
 
Posts: 3344
Joined: 25. April 2006 17:06

Re: Error accessing MySQL database

Postby NinjaGaid3n » 12. February 2009 04:07

Thankyou for your assistance. I have made another user, called Users which i am using, i gave that username all privilages. And have a database i created containing.

CREATE TABLE `users` (
`UserID` INT(25) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`Username` VARCHAR(65) NOT NULL ,
`Password` VARCHAR(32) NOT NULL ,
`EmailAddress` VARCHAR(255) NOT NULL
);

the page it is linked to still is not loading. I will carry on with this tomorrow though now, as I am tired lol. its 3 am here. So, I will go throught that thread link you provided then. Thankyou for you help.

Will post again tomorrow on how I have gotten on.
NinjaGaid3n
 
Posts: 3
Joined: 11. February 2009 16:59

Re: Error accessing MySQL database

Postby Izzy » 12. February 2009 04:21

Also paste the code that tries to access the database in your php file that won't load the database.
Or are you having problems loading any php file?

Is the php file one that you created or is it part of a downloaded script?
Izzy
 
Posts: 3344
Joined: 25. April 2006 17:06

Re: Error accessing MySQL database

Postby NinjaGaid3n » 12. February 2009 04:32

I am only having problems with this particular php file as i tested another php file and it loaded. I was using a tutorial to create the files. It is a log in and registration setup in php making use of MySQL.

the code linking the php files to the database are:

this code is setup in a php file called base.php:

<?php
session_start();

$dbhost = "localhost"; // this will ususally be 'localhost', but can sometimes differ
$dbname = "users"; // the name of the database that you are going to use for this project
$dbuser = "Ben"; // the username that you created, or were given, to access your database
$dbpass = ""; // the password that you created, or were given, to access your database

mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
?>


that file is linked to from the Login.php and registration.php files. at the top of the page before any other code "

<?php include "base.php"; ?>

"
index.php gets information from databse with :

<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
?>

<h1>Member Area</h1>
<pThanks for logging in! You are <b><?=$_SESSION['Username']?></b> and your email address is <b><?=$_SESSION['EmailAddress']?></b>.</p>

<?php
}
elseif(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysql_real_escape_string($_POST['username']);
$password = md5(mysql_real_escape_string($_POST['password']));

$checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");

if(mysql_num_rows($checklogin) == 1)
{
$row = mysql_fetch_array($checklogin);
$email = $row['EmailAddress'];

$_SESSION['Username'] = $username;
$_SESSION['EmailAddress'] = $email;
$_SESSION['LoggedIn'] = 1;

echo "<h1>Success</h1>";
echo "<p>We are now redirecting you to the member area.</p>";
echo "<meta http-equiv='refresh' content='=2;index.php' />";
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your account could not be found. Please <a href=\"index.php\">click here to try again</a>.</p>";
}
}
else
{
?>


and registration enters information into database with:

<?php
if(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysql_real_escape_string($_POST['username']);
$password = md5(mysql_real_escape_string($_POST['password']));
$email = mysql_real_escape_string($_POST['email']);

$checkusername = mysql_query("SELECT * FROM users WHERE Username = '".$username."'");

if(mysql_num_rows($checkusername) == 1)
{
echo "<h1>Error</h1>";
echo "<p>Sorry, that username is taken. Please go back and try again.</p>";
}
else
{
$registerquery = mysql_query("INSERT INTO users (Username, Password, EmailAddress) VALUES('".$username."', '".$password."', '".$email."')");
if($registerquery)
{
echo "<h1>Success</h1>";
echo "<p>Your account was successfully created. Please <a href=\"index.php\">click here to login</a>.</p>";
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your registration failed. Please go back and try again.</p>";
}
}
}
else
{
?>



sorry if i put too much stuff there. Just thought more information is better than being vague.

thankyou
NinjaGaid3n
 
Posts: 3
Joined: 11. February 2009 16:59

Re: Error accessing MySQL database

Postby Izzy » 12. February 2009 05:10

sorry if i put too much stuff there. Just thought more information is better than being vague.
Oh so very true and no need to apologise as I wish more would do that when asking for assistance.


Refer to my link re how to do this, as most info is as you have posted so long as it matches those settings in phpMyAdmin for the users database.
Code: Select all
$dbhost = "localhost"; // this will ususally be 'localhost', but can sometimes differ
$dbname = "users"; // the name of the database that you are going to use for this project
$dbuser = "Ben"; // the username that you created, or were given, to access your database
$dbpass = ""; // the password that you created, or were given, to access your database

Missing is a $dbpass for Ben - no need to post it here if you feel it would be insecure as long as there is one.

Your script error is also telling you that there is no password.

I think if you follow my little tut, (even if you create another database, you can drop it when finished with the tut), then you may see what this database creation and the requirements is all about - you seem to be very close now.

Someone with more php knowledge may be able to spot any issues with the rest of the code.
Izzy
 
Posts: 3344
Joined: 25. April 2006 17:06


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 44 guests