简体   繁体   English

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

[英]Unable to connect to MySQL database using XAMPP

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. 我是编程新手,我一直在尝试从Murachs PHP和MySQL指导书中运行一个PHP应用程序,源代码实际上来自他们的网站,但收到错误消息。 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)" “数据库错误连接数据库时出错。必须按照附录中的说明安装数据库。必须按照第1章中的说明运行MySQL。错误消息:SQLSTATE [HY000] [1045]用户'mgs_user'的访问被拒绝@'localhost'(使用密码:是)”

I downloaded XAMPP and it is running fine but for some reason I am unable to use the MySQL database. 我下载了XAMPP,它运行正常,但是由于某种原因,我无法使用MySQL数据库。 I have searched and Googled and searched some more for the last two days but I can't seem to find a solution. 我搜索了Google并搜索了过去两天的更多信息,但似乎找不到解决方案。

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? 我记得在安装过程中设置了用户名和密码,昨天我通过PHPMyAdmin设置了名为“ root”的用户名和密码,但是为什么该应用程序无法运行?

Any help is appreciated. 任何帮助表示赞赏。

Update #1 更新#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. 这是从Murach的网站下载的,因此代码应该不错。 I am thinking there is something wrong with the XAMPP/MySQL that will not allow a connection to the database. 我认为XAMPP / MySQL出现问题,将不允许连接到数据库。

/*****************************************
* 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 更新#2

I created the user "mgs_user@localhost" in PHPMyAdmin and gave it permissions and now I get the following error; 我在PHPMyAdmin中创建了用户“ mgs_user @ localhost”,并为其授予了权限,现在出现以下错误;

"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. MySQL必须按照第1章所述运行。

Error message: SQLSTATE[HY000] [1049] Unknown database 'my_guitar_shop1' " 错误消息:SQLSTATE [HY000] [1049]未知数据库'my_guitar_shop1'“

The code seems to be correct but it is not creating users or databases in MySQL. 该代码似乎是正确的,但是没有在MySQL中创建用户或数据库。

Update #3 更新#3

Created the database "my_guitar_shop1" through PHPMyAdmin and now I get these errors with corresponding code just below each error message; 通过PHPMyAdmin创建了数据库“ my_guitar_shop1”,现在我在每个错误消息的下面获取了这些错误以及相应的代码;

( ! ) Notice: Undefined index: category_id in C:\\xampp\\htdocs\\book_apps\\ch05_guitar_shop\\product_manager\\index.php on line 16 (!)注意:未定义的索引:第16行的C:\\ xampp \\ htdocs \\ book_apps \\ ch05_guitar_shop \\ product_manager \\ index.php中的category_id

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 (!)致命错误:在第15行的C:\\ xampp \\ htdocs \\ book_apps \\ ch05_guitar_shop \\ model \\ category_db.php的非对象上调用成员函数fetch()

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;
}

The most likely reason is that you didn't created the 'mgs_user' user, for this you can do something like this: 最可能的原因是您没有创建'mgs_user'用户,为此您可以执行以下操作:

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';

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. 最初从本书开始时,我遇到了同样的问题,错误消息给出的说明“按照第1章中的说明”没有任何帮助。 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. 但是,如果您转到本书背面的附录,则会详细说明如何通过phpMyAdmin运行脚本,该脚本将创建练习所需的适当数据库。 If you're running Windows, the details are on page 818 and it cleared up the problem for me. 如果您运行的是Windows,则详细信息在第818页上,它为我解决了问题。 Hope that helps. 希望能有所帮助。

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

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