PHP - MySQL Access Denied Error - Works in Other Programs

PHP - MySQL access denied error - Works in other programs

You have 'root[SPACE]' as the user name. Try 'root' without the space?

Access Denied Connecting to MySQL with PHP

The issue was that I had installed php separately, and made my database, then installed XAMPP and tried to connect to that database. The proper way is to create the database using php my admin, which comes with XAMPP, and has it's own php, MySQL, and apache that you must use for everything to work.

Why am I getting an access denied error when connecting to MySQL db from php page

Your code is giving you a credentials problem (detected by your "Check connection" piece of your code), and that is derived of the users configuration in your MySQL Workbench. You need to configure the user you will use for your PHP connection to MySQL, along with the password, and the server the user connecting will be limited to (in this case is the localhost), and the privileges your user will have.

Also, and this is just for connecting to your database (for now consider that you won't execute a SQL query in your code), you can check if the connection it's ok with just your username, the password and the server name.

<?php
/* $servername, $username and $password goes here */

$conn = new mysqli($servername, $username, $password);

if($conn->connect_error) {
die("Connection failed:" $conn->connect_error);
}

echo "Connection successful";
?>

PHP MySQL - Access denied for user

Aside from the fact that you should be using mysqli functions instead of mysql for security reasons, have you checked:

  • Your MySQL username is correct?
  • That a password is not required?
  • The database you are connecting to is correct?

The do:

$db = mysql_connect("localhost", "root", "");
mysql_select_db("MyDB", $db);

And your queries should be:

$query = mysql_query("SELECT * FROM users WHERE uname = '$uname'", $db);

MySQL - Access denied when connecting locally but able to connect remotely

There's two options: Either you are binding to the remote IP and not the local one or you messed up the permissions.

1. Binding the IP adress

If you followed an online tutorial on how to allow remote access, you probably set the config option bind-adress to you axternal IP. This means, your server will bind only externally but not on the internal loopback (127.0.0.1/localhost).

All you have to do is:

  1. Open your config file (usually /etc/mysql/my.cnf) with root permissions
  2. Locate the setting bind-adress and set it to 0.0.0.0

2. Permissions

Try granting the permission through the terminal to '<your user>'@'%'.
If you don't understand what this means, read the GRANT manual here.

Here's an example for giving superuser permissions to a user from anywhere for every database:

GRANT ALL PRIVILEGES ON *.* TO '<your user>'@'%' WITH GRANT OPTION;

I wouldn't recommend using the above on a production server since it is a big security risk.

Access Denied Error in PHP/MySQL Script

You are missing the mysql_connect() call.

You are saying that you called it in another file which redirected to this page, but even though it was connected in the previous page, it is not connected in this page. So you have to call mysql_connect separately on this page, or include the file that does the calling.



Related Topics



Leave a reply



Submit