简体   繁体   English

PHP连接到MySQL-访问被拒绝

[英]PHP connecting to mySQL - Access denied

I have the following bit of code: 我有以下代码:

ob_start();
$host="localhost"; // Host name
$username="admin"; // Mysql username
$password=""; // Mysql password
$db_name="my_db_m"; // Database name
$tbl_name="members"; // Table name

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

// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

When I attempt to login, I get: cannot select DB :Access denied for user ''@'localhost' to database 'my_db_m' 尝试登录时,我得到: 无法选择数据库:拒绝用户“ @'localhost”访问数据库“ my_db_m”

I am using xampp. 我正在使用xampp。 I used phpMyAdmin to create a database called 'my_db_m'. 我使用phpMyAdmin创建了一个名为“ my_db_m”的数据库。 I created a table named 'members'. 我创建了一个名为“ members”的表。

Am I missing something? 我想念什么吗? I am connecting to the mysql server yet unable to access the database... 我正在连接到mysql服务器,但无法访问数据库...

You probably need to grant access to your admin user. 您可能需要向您的admin用户授予访问权限。

GRANT ALL ON my_db_m.* TO 'admin'@'localhost';

This is quite sweeping as it grants all rights, and you might want to restrict access for general use to something like 因为它授予了所有权利,所以这非常笼统,您可能希望将一般用途的访问权限限制为

GRANT INSERT, UPDATE, SELECT, DELETE ON my_db_m.* TO 'admin'@'localhost';

Simple 简单

GRANT USAGE ON my_db_m TO user@localhost IDENTIFIED BY 'passwd';

If you'd like to grant usage to any DB use: 如果您想为任何数据库使用授予使用权:

GRANT USAGE ON *.*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM