I read the message board notice regarding common questions. I know that this one is common but I have reviewed all related posts on stackoverflow and can't find a solution to this problem.
I am using the latest MAMP. I have a mysql database which I set up in the command line using the mysql which came with MAMP. I have a user named "phpuser" whose password is "phpuser". I have checked this many times and can log in with that user and password by the command line.
Here is my code. It is straight out of the intro tutorial here (http://www.w3schools.com/php/php_mysql_connect.asp):
<?php
$servername = "localhost";
$username = “phpuser”;
$password = “phpuser”;
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
When I run it using the most recent MAMP php version, I get this error:
Nicks-MacBook-Pro:howdy Nick$ /Applications/MAMP/bin/php/php5.6.2/bin/php first_db.php
Notice: Use of undefined constant “phpuser” - assumed '“phpuser”' in /Applications/MAMP/htdocs/howdy/first_db.php on line 3
Notice: Use of undefined constant “phpuser” - assumed '“phpuser”' in /Applications/MAMP/htdocs/howdy/first_db.php on line 4
Warning: mysqli::mysqli(): (28000/1045): Access denied for user '“phpuser”'@'localhost' (using password: YES) in /Applications/MAMP/htdocs/howdy/first_db.php on line 7
Connection failed: Access denied for user '“phpuser”'@'localhost' (using password: YES)
I've tried running it with the default php version and then I get a different error:
Nicks-MacBook-Pro:howdy Nick$ php first_db.php
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user '“phpuser”'@'localhost' (using password: YES) in /Applications/MAMP/htdocs/howdy/first_db.php on line 7
Connection failed: Access denied for user '“phpuser”'@'localhost' (using password: YES)
I've tried it with 127.0.1.1 instead of local host and that doesn't work either. I've checked my host file and that seems fine. Here is is anyway:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
I've also tried running the sample code from the MAMP documentation here: http://www.mamp.info/en/documentation/MAMP-3-User-Guide.pdf
I run the following:
<?php
DEFINE('DB_USERNAME', 'phpuser');
DEFINE('DB_PASSWORD', 'phpuser');
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_DATABASE', 'status');
// http://www.php.net/manual/en/mysqli.connect.php
$mysqli = new mysqli(DB_HOST, DB_USERNAME,
DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno()
. ') ' . mysqli_connect_error());
}
echo 'Connected successfully.';
$mysqli->close();
?>
And I get the following error:
Nicks-MacBook-Pro:howdy Nick$ /Applications/MAMP/bin/php/php5.6.2/bin/php first_db.php
Notice: Use of undefined constant ‘phpuser’ - assumed '‘phpuser’' in /Applications/MAMP/htdocs/howdy/first_db.php on line 3
Notice: Use of undefined constant ‘phpuser’ - assumed '‘phpuser’' in /Applications/MAMP/htdocs/howdy/first_db.php on line 4
Notice: Use of undefined constant ’status’ - assumed '’status’' in /Applications/MAMP/htdocs/howdy/first_db.php on line 6
Warning: mysqli::mysqli(): (28000/1045): Access denied for user '‘phpuser’'@'localhost' (using password: YES) in /Applications/MAMP/htdocs/howdy/first_db.php on line 9
Connect Error (1045) Access denied for user '‘phpuser’'@'localhost' (using password: YES)
I've tried other tutorials out there and all of them return errors. I tried using the define() function instead of the variables but that doesn't seem to work. I've read through several threads regarding the "Use of undefined constant" problem but I can't see how any of the solutions apply to me here.
I also tried adding a link to the mysql.socket using the following code. This didn't help me either (I didn't have a /var/mysql file to start).
sudo mkdir /var/mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock
I've also tried playing with different mysql sockets and couldn't get any of the codes to run without errors.
The quotes (“) you are using around phpuser look like special characters (copy-paste from word?)... try to replace them with (").