Connecting PHP to mySQL on osX Sierra

I’m quite new to PHP and mySQL. I’m using the Mac os Sierra pre installed copies of Apache and PHP. I’ve downloaded and configure mySQL (works absolutely fine, able to use CRUD and so on). However, I’ve been struggling to connect from php to the database. I have this code:

    $dbhost = "localhost";
    $dbuser = "mgs";
    $dbpass = "pass1";
    $dbname = "widget_corp";
    $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

        die("Database connection failed " .
             mysqli_connect_error() .


<!doctype html>







I’ve saved it as a .php file
However I keep getting this error when i try to open the .php file from a browser:

Warning: mysqli_connect(): (HY000/2002): No such file or directory in /Users/Mahfouz/Sites/connect_prac.php on line 6
Database connection failed No such file or directory(2002)

I’ve created the User and Database referenced in the code.
what am I doing wrong? how do I get this working?

PS: i’ve looked at similar questions on this website, but I haven’t been able to apply any of the given solutions to my situation.

Source: stackoverflow-php