Unable to connect to MySQL database using XAMPP

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Unable to connect to MySQL database using XAMPP

Postby St Pete Design » 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.

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?
St Pete Design
 
Posts: 3
Joined: 25. November 2013 22:57
Operating System: Windows 7

Re: Unable to connect to MySQL database using XAMPP

Postby St Pete Design » 26. November 2013 15:07

:cry:
This is day 3 of trying to get XAMPP to work.

I have heard so many good things about it but for me it has been a nightmare. I am just starting to learn PHP progamming and I am completely unable to run PHP on my XAMPP set up.

It works great if you are using CSS and XHTML but as soon as I try to use the MySQL database it does not work.
St Pete Design
 
Posts: 3
Joined: 25. November 2013 22:57
Operating System: Windows 7

Re: Unable to connect to MySQL database using XAMPP

Postby JJ_Tagy » 26. November 2013 15:38

Sounds like you are trying to do multiple things at once instead of taking it one step at a time. Either that or you put so much in your first post you are obfuscating your current problem set.

PHP and MySQL work very well together in XAMPP. You will find that regardless of the stack you use (independent or AIO package like XAMPP).

Your first box is a notice that category_id is empty. Either it wasn't passed at all or it was blank. Therefore, when the SQL code is run, the result will obviously be 0 rows which means $category will be null or empty. Fetch is the first time you try to access the results with a fatal exit meaning that there aren't any debug or error catching codes prior to execution.

You also talk about creating users and databases. Are you trying to do that through PHP? I don't see where you granted that access to mgs_user. Have you verified exactly what you are trying to do is inserted into the database?

Try starting fresh one step at a time. Stop at each error and work through the problem before moving on to a full package.
JJ_Tagy
 
Posts: 788
Joined: 30. January 2012 13:44
XAMPP version: 5.5.15
Operating System: Windows 10 Pro x64

Re: Unable to connect to MySQL database using XAMPP

Postby St Pete Design » 26. November 2013 16:39

"You also talk about creating users and databases. Are you trying to do that through PHP? I don't see where you granted that access to mgs_user. Have you verified exactly what you are trying to do is inserted into the database?"

That's the problem. The PHP Application that I am running grants "mgs_user" the necessary permissions but it does not show up in PHPMyAdmin, so I created the user manually and gave it the permissions through PHPMyAdmin. The PHP Application is also supposed to create databases but it does not work.

The code that I am using is downloaded directly from Murachs so it should be good. Murachs is an instructional book for learning programming.

Let me be clear that I am literally just learning PHP and that is why I need XAMPP so that I can run the applications from the book that I am using to learn PHP and MySQL.
St Pete Design
 
Posts: 3
Joined: 25. November 2013 22:57
Operating System: Windows 7

Re: Unable to connect to MySQL database using XAMPP

Postby JJ_Tagy » 26. November 2013 18:17

Just because they are published doesn't mean you are using it correctly or the versions match. Sometimes things change (especially with PHP versions). Too many variables to guess.

So lets look at how you are creating a user. Using PHP, you must have a username/password to access MySQL. It will usually have limited access to a specific database unless you are using 'root'. Reading above, the user in question must have the proper permissions to create other users if that is indeed what you are doing. Otherwise, it would be insecure because any PHP code sneaked into your files could compromise your security. Perhaps you are creating a user within the application's own database? That would be a different scenario.

So what PHP code are you using to access the MySQL and create a user?
JJ_Tagy
 
Posts: 788
Joined: 30. January 2012 13:44
XAMPP version: 5.5.15
Operating System: Windows 10 Pro x64

Re: Unable to connect to MySQL database using XAMPP

Postby Altrea » 27. November 2013 06:32

It is very uncommon that a webapplication itself creates its own database users and databases in its php script. That will not work on most webhosts because of missing priviledges.

With XAMPP it should work (with the correct user and permissions), but we can't say anything about that. I didn't buy this book myself and don't know the text and code inside.
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Unable to connect to MySQL database using XAMPP

Postby lusherpj » 04. January 2016 23:36

I too came across this as I'm going through the same textbook. Even though the question is old, it is very specific with an easy solution. This pertains to Chapter 1 in Murach's PHP and MySQL. So, if we're looking this up, we don't know very much at all...yet.

I was able to get this fixed with the help of my instructor. Be sure to complete all setup from all sections of the Appendix. The section "How to create and restore the databases" will fix this issue (page 818 for Windows, 834 for Mac).
lusherpj
 
Posts: 1
Joined: 04. January 2016 23:32
Operating System: Mac

Re: Unable to connect to MySQL database using XAMPP

Postby Stebro » 13. March 2018 01:48

For the absolute newbies:

If your version of XAMPP does not have phpMyAdmin, you will have to install it separately. You can download it at Apache Friends.

Open phpmyadmin.

Navigate to the IMPORT button and select it. (This will open a new page.)
You can use the defaults, but check to make sure the format selected is SQL.
Navigate to the BROWSE button and select it.
Use the tool to navigate to ...\XAMPP\htdocs\book_apps\create_db\
Inside that folder you will find three .sql files.
Select one (maybe you can select them all, but I haven't tried it) and hit <enter>.
At the bottom of the import page is a button GO. Select it and the file will be imported into MySql.
Repeat as necessary.

That's all there is to it. Murach could have been clearer and maybe they are ... in the third edition. As mentioned above, this is broadly described on pages 818,819. Good luck. This kind of thing can be maddening, I know.
Stebro
 
Posts: 1
Joined: 13. March 2018 01:23
XAMPP version: 3.2.2
Operating System: Windows 7, 10 Ubuntu 16.04


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 127 guests