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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器