Problem connecting to MySQL database using PHP

Problems with the Mac OS X version of XAMPP, questions, comments, and anything related.

Problem connecting to MySQL database using PHP

Postby paddys_1 » 26. March 2011 00:17

I am trying to connect to a mysql database using php. I created a database called 'dbone' with phpmyadmin and then created a table called 'contacts' inside this database. The username for the database is 'testuser' and the password is 'testpassword'.

I then tried to connect to the database file using the following .php file:

<?
$user="testuser";
$password="testpassword";
$database="dbone";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="CREATE TABLE contacts (id int(6) NOT NULL auto_increment,first varchar(15) NOT NULL,last varchar(15) NOT NULL,phone varchar(20) NOT NULL,mobile varchar(20) NOT NULL,fax varchar(20) NOT NULL,email varchar(30) NOT NULL,web varchar(30) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
mysql_query($query);
mysql_close();
?>


I saved the above file in the following location:

Applications/XAMPP/xamppfiles/htdocs/test.php

I then typed the following into my browser to try to connect to the database:

http://localhost/test.php

The following error message then came up in the browser window:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'testuser'@'localhost' (using password: YES) in /Applications/XAMPP/xamppfiles/htdocs/test.php on line 5
Unable to select database


Could anyone tell me what I am doing wrong and how can I connect to the database using php?
paddys_1
 
Posts: 1
Joined: 26. March 2011 00:08

Re: Problem connecting to MySQL database using PHP

Postby MegaChriz » 29. March 2011 08:05

Although this is not the cause of the error, it's better to type 'localhost' between quotes:
Code: Select all
mysql_connect('localhost', $user, $password);
This way you declare it as a string to PHP. Otherwise it could also be a constant (although it's common to define constants in uppercase).

The error tells something went wrong when mysql_connect() was called:
Code: Select all
Access denied for user 'testuser'@'localhost' (using password: YES)
This can mean three things:
- The user 'testuser' is unknown.
- The password for user 'testuser' is not right.
- You try to connect to the wrong server. This cause is not likely, because with a XAMPP installation it's common to connect to 'localhost'.
So you should ensure that the username and it's password are right. Maybe you made a typo when you defined the password for the user 'testuser'?

Another thing: it's better to write '<?php' instead of '<?' as short open tags are not supported by every server. Your code will now be outputted instead of processed if the short open tags are not supported by the server.
MegaChriz
 
Posts: 158
Joined: 16. February 2009 15:04
Location: Amersfoort, The Netherlands
XAMPP Version: 1.8.3-3
Operating System: Mac OS X


Return to XAMPP for Mac OS X

Who is online

Users browsing this forum: No registered users and 2 guests