Reputation: 1
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;
}
Upvotes: 0
Views: 12135
Reputation: 1
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.
Upvotes: 0
Reputation: 2817
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';
Upvotes: 1