0

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?

RGriffiths
  • 5,722
  • 18
  • 72
  • 120
  • 1
    And I don't mind a down vote if it is explained why? – RGriffiths Jul 30 '22 at 10:49
  • 1
    I think this has nothing to do with the mysql connection, it's a PHP problem. I have never encountered it myself, but you can find suggestions online. For instance: [How To Fix Bad Interpreter No Such File or Directory Error](https://www.positioniseverything.net/bad-interpreter-no-such-file-or-directory/). I am absolutely not sure this link is useful... – KIKO Software Jul 30 '22 at 10:50
  • Thanks - I have been looking for days to get around this. Update to the error message opening a new terminal. – RGriffiths Jul 30 '22 at 10:59
  • 3
    You seem to be getting closer. I read that the default port on MAMP for mysql is 8889. So perhaps it is: `127.0.0.1:8889`? This wouldn't explain why the code would work when ran it through the server though. – KIKO Software Jul 30 '22 at 11:08
  • Well that is amazing. I honestly thought that I had tried that at some point over the last few days. Wonderful - thanks a million. – RGriffiths Jul 30 '22 at 11:12
  • Nice to hear that it worked. – KIKO Software Jul 30 '22 at 11:21

0 Answers0