Access denied for user

Hello all,

I am fairly new to PHP and have been frustrated over the past few days trying to resolve a Access deinied to user a3668500@localhost (using password: NO). Im using mysql_connect and my sql_query. I know they have been depricated and plan on converting to mysqli but I I would really like to figure out my current issue first.

Here is my php code which established a connection.


public function connect() {
		require_once 'include/config.php';
		// connecting to mysql
		$con = mysql_connect("mysql13.000webhost.com", "a3668500_mbrooks", "xxxxxxxxx", "a3668500_android");
	        mysql_select_db("a3668500_android", $con);
                //return database handler
		return $con;
}

And here is my query code. It is telling me that the error comes from mysql_query line but I from what I understand its probably because Im not properly forming a connection.


public function isUserExisted($email) { 
        $query = "SELECT email from users WHERE email = '$email'";
        $result = mysql_query($query);
        $no_of_rows = mysql_num_rows($result);
        echo mysql_error();
        if ($no_of_rows > 0) {
            // user existed
            return true;
        } else {
            // user not existed
            return false;
        }
}

Is it possible that I could receive this error if there is a problem with my table or one of it’s fileds? On another note, does anyone have any good recommendations for a php debugger? Right now, I am just using the error messages that I am getting in the android LogCat of eclipse.

" Access deinied to user a3668500@localhost (using password: NO)." means you aren’t inputting a password when trying to connect. Have you checked your variable names and values properly?

Also please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn’t tie you down so much to a particular database server software.

Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.

Ditto to this, I wonder why there so many people posting their code written with the old fashined mysql functions. Use MySQLi and PDO is the way of future, I doubt many of them are working on legacy code though.

As soon as I can get this error figured out, I will switch over to to pdo and prepared statements. I know the error is telling me that there is no password being sent to when trying to login. Just to make sure it was not a screw up with a variable name, I put the password directly in the mysql_connect function. My above example is not correct that. Those are the parameters for mysqli. Here is the connection call Im using.


// Connecting to Database
	public function connect() {
		require_once 'include/config.php';
		// connecting to mysql
		$con = mysql_connect("mysql13.000webhost.com", "a3668500_mbrooks", "xxxxxxxxxx");
	        mysql_select_db("a3668500_android", $con);
                //return database handler
		return $con;
	}

Probably a good few are adapting code from tutorials, some of them tutorials may have been around on the net for a good few years and might not necessarily have been updated.

@mbrooks78; Load up phpMyAdmin and click on the “Users” tab. Click on the Edit Privileges link for the “a3668500_mbrooks” user and check that they have the privilges for accessing the “a3668500_android” database and check that the password for the "“a3668500_mbrooks” user matches the password that you’re using in your code.

@SpacePhoenix I have a users tab but It just brings me to the table. I do not see an option for editing privilages. Here is a screen shot of what it looks like.

Im using free hosting through 000webhost and I recently found out that they restrict remote sql connectins for free acounts. Could this be my problem?

It could be, but how much traffic/bandwidth does your site use? If you do not receive a lot of visitors this shouldnt be a problem.

They do not allow remote connections and the sql server isnt for a website at the moment. It is for an android app that Im working on which would need to create accounts and login through the webserver and sql.

As your on a free hosting, the hosting company has probably locked down MySQL so it’s very doubtful that you’ll be able to edit user privileges. If you’re still developing the site/app then you could download and install Apache, MySQL and PHP locally for developing and testing until you’re ready for beta testing. You can get them individually or “canned” together using XAMP, LAMP or WAMP