Unable to connect to MySQL database using XAMPP
Posted: 25. November 2013 23:16
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 I 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.
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
( ! ) 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
Why am I unable to create databases and new users with PHP code?
"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.
- Code: Select all
/*****************************************
* 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
- Code: Select all
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
- Code: Select all
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;
}
Why am I unable to create databases and new users with PHP code?