On a Mac I have MAMP installed. Running the following from the browser:
<?php
$conn = mysqli_connect('localhost', 'dbuser', 'password', 'testdb');
if ($conn) {
echo "Connected successfully";
}
else {
die("Connection failed: " . mysqli_connect_error());
}
and it connects.
But if I invoke the same code via terminal it fails:
#!/usr/bin/env php
<?php
$conn = mysqli_connect('localhost', 'dbuser', 'password', 'testdb');
if ($conn) {
echo "Connected successfully";
}
else {
die("Connection failed: " . mysqli_connect_error());
}
In the terminal I get the message:
PHP Warning: mysqli_connect(): (HY000/2002): No such file or directory in /Applications/MAMP/htdocs/testsite/con on line 8
Where line 8 is the mysqli_connect
Using 127.0.0.1 or 127.0.0.1:3306 I get:
PHP Warning: mysqli_connect(): (HY000/2002): Connection refused in /Applications/MAMP/htdocs/testsite/con on line 8
Warning: mysqli_connect(): (HY000/2002): Connection refused in /Applications/MAMP/htdocs/testsite/con on line 8
Connection failed: Connection refusedMac:testdb
Any ideas how I fix this?