无法使用XAMPP连接到MySQL数据库

I am new to programming and I have been trying to run a PHP application from Murachs PHP and MySQL instructional book, the source code is actually from their website, but have been getting an error message. The error message is as follows;

"Database Error There was an error connecting to the database. The database must be installed as described in the appendix. MySQL must be running as described in chapter 1. Error message: SQLSTATE[HY000] [1045] Access denied for user 'mgs_user'@'localhost' (using password: YES)"

I downloaded XAMPP and it is running fine but for some reason I am unable to use the MySQL database. I have searched and Googled and searched some more for the last two days but I can't seem to find a solution.

I remember setting up a username and password during installation and I set up a username called "root" and a password through PHPMyAdmin yesterday but why will this application not run?

Any help is appreciated.

Update #1

Here are samples of the code that creates the database, the user and the password.

This is downloaded from Murach's site so the code should be good. I am thinking there is something wrong with the XAMPP/MySQL that will not allow a connection to the database.

/*****************************************
* Create the my_guitar_shop1 database
*****************************************/
DROP DATABASE IF EXISTS my_guitar_shop1;
CREATE DATABASE my_guitar_shop1;
USE my_guitar_shop1;  -- MySQL command

-- Create a user named mgs_user
GRANT SELECT, INSERT, UPDATE, DELETE
ON *
TO mgs_user@localhost
IDENTIFIED BY 'pa55word';

Update #2

I created the user "mgs_user@localhost" in PHPMyAdmin and gave it permissions and now I get the following error;

"Database Error There was an error connecting to the database.

The database must be installed as described in the appendix.

MySQL must be running as described in chapter 1.

Error message: SQLSTATE[HY000] [1049] Unknown database 'my_guitar_shop1' "

The code seems to be correct but it is not creating users or databases in MySQL.

Update #3

Created the database "my_guitar_shop1" through PHPMyAdmin and now I get these errors with corresponding code just below each error message;

( ! ) Notice: Undefined index: category_id in C:\xampp\htdocs\book_apps\ch05_guitar_shop\product_manager\index.php on line 16

if ($action == 'list_products') {
// Get the current category ID
$category_id = $_GET['category_id'];    <-----this is line #16
if (!isset($category_id)) {
    $category_id = 1;
}

( ! ) Fatal error: Call to a member function fetch() on a non-object in C:\xampp\htdocs\book_apps\ch05_guitar_shop\model\category_db.php on line 15

function get_category_name($category_id) {
global $db;
$query = "SELECT * FROM categories
          WHERE categoryID = $category_id";
$category = $db->query($query);
$category = $category->fetch();                <-----this is line #15
$category_name = $category['categoryName'];
return $category_name;
}
dsn5510
dsn5510 如果我想以“root”身份连接,我需要在几个不同的文件中更改应用程序代码。PHP代码不应该能够创建有权访问数据库的新用户吗?
接近 7 年之前 回复
dsa4214
dsa4214 您正在使用连接作为mgs_user。也许你应该以root身份连接。请告诉我们您连接到数据库的行,并从中删除您的密码。
接近 7 年之前 回复

2个回答



最可能的原因是你没有创建'mgs_user'用户,为此你可以这样做:</ p>

 创建用户'mgs_user'@'localhost'识别'密码'; 
</ code> </ pre>

然后使用相应的权限授予权限 :</ p>

 在databaseName上授予所有特权。*到“mgs_user”@“localhost”IDENTIFIED BY'password'; 
</ code> </ pre>
</ DIV>

展开原文

原文

The most likely reason is that you didn't created the 'mgs_user' user, for this you can do something like this:

CREATE USER 'mgs_user'@'localhost' IDENTIFIED BY 'password';

Then grant the appropriate permissions using:

GRANT ALL PRIVILEGES ON databaseName.* To 'mgs_user'@'localhost' IDENTIFIED BY 'password';

duannao3402
duannao3402 XAMPP工作得很好,问题是你的应用程序数据库没有正确创建,如果有的话,你需要自己运行所有的数据库创建脚本,无论是在MySQL的命令提示符还是在PHPMyAdmin中。
接近 7 年之前 回复
dpfz27768
dpfz27768 这仍然是同一问题的一部分。 XAMPP不起作用。 为什么它没有正确运行代码?
接近 7 年之前 回复
duan5362
duan5362 顺便说一句,请为每个问题发布一个问题,这对您和一般网站都有好处。
接近 7 年之前 回复
doulutian4843
doulutian4843 这个错误(调用非对象上的成员函数fetch())通常意味着返回的查询和空集或失败,我建议你检查query()的结果,无论如何,很可能是那个表 此示例依赖于,因为数据库和用户也未创建。
接近 7 年之前 回复
duangou2028
duangou2028 你的意思是用PHPMyAdmin创建数据库? 我刚刚这样做了,现在我收到了上面发布的另一个错误。 顺便感谢Rafael到目前为止的帮助。
接近 7 年之前 回复
doujiao1949
doujiao1949 您发布的代码正在运行,但很可能以前没有调用过,因为您已经有了创建数据库的代码,只需在PHPMyAdmin上运行查询即可创建它。
接近 7 年之前 回复
dtt27783
dtt27783 我检查了PHPMyAdmin,用户“mgs_user @ localhost”不在那里。 我创建了用户,现在我收到一条错误,上面写着“未知数据库”。 我将在上面发布。 是什么赋予了? 我不明白为什么代码不起作用。
接近 7 年之前 回复
duanlongnao0028
duanlongnao0028 好吧,MySQL代码很好,如果你是MySQL控制台精明,那么我建议你检查用户是否真的在那里,通过从MySQL控制台连接这个用户或者以root身份连接并查看用户及其权限 。
接近 7 年之前 回复
dongyou2305
dongyou2305 我发布了创建用户的代码。 这再次来自Murach的网站。 它看起来应该有效吗?
接近 7 年之前 回复



我最初从本书开始时遇到了同样的问题,错误信息给出的说明是“遵循第1章中的说明” 没有任何帮助。 但是,如果您转到本书后面的附录,它会详细说明如何通过phpMyAdmin运行脚本,该脚本将创建练习所需的相应数据库。 如果您正在运行Windows,详细信息请参见第818页,它为我解决了问题。 希望有所帮助。</ p>
</ div>

展开原文

原文

I had the same problem when initially starting with the book, and the description the error message gives to "follow the instructions in chapter 1" isn't any help. If you go to the appendix at the back of the book, however, it gives a detailed explanation on how to run a script through phpMyAdmin that will create the appropriate databases needed for the exercises. If you're running Windows, the details are on page 818 and it cleared up the problem for me. Hope that helps.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐