3

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:

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

    if(mysqli_connect_errno()){
        die("Database connection failed " .
             mysqli_connect_error() .
             "(".mysqli_connect_errno().")"
           );
    }

?>

<!doctype html>

    <html>
        <head>
            <title>Conn</title>

        </head>

        <body>


        </body>

    </html>
<?php
    mysqli_close($connection);

?>

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? thanks

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.

Asif Patel
  • 1,744
  • 1
  • 20
  • 27
MGS
  • 456
  • 1
  • 9
  • 24
  • http://stackoverflow.com/questions/1676688/php-mysql-connection-not-working-2002-no-such-file-or-directory – Chris Cynarski Feb 17 '17 at 22:16
  • "CRUD" is something you do with a web stack as it refers specifically to [REST](https://en.wikipedia.org/wiki/Representational_state_transfer). It can't be done with MySQL alone. Now before you get too far into `mysqli`, do try and use the [object-oriented interface](http://php.net/manual/en/mysqli.quickstart.dual-interface.php) via `new mysqli(...)` and not the obsolete PHP 4 era procedural one. It makes errors much more obvious. Also [turn on exception reporting](http://stackoverflow.com/questions/23816633/mysqli-not-throwing-errors-why). – tadman Feb 17 '17 at 22:32

1 Answers1

4

I followed these steps and it worked:

cd /var mkdir mysql cd mysql ln -s /tmp/mysql.sock mysql.sock

gotten from: https://jason.pureconcepts.net/2016/09/install-apache-php-mysql-mac-os-x-sierra/

MGS
  • 456
  • 1
  • 9
  • 24