doumou8527 2013-11-25 19:37
浏览 449

无法使用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;
}
  • 写回答

2条回答 默认 最新

  • dtrnish3637 2013-11-25 19:46
    关注

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

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功