0
$db= mysqli_connect('localhost', "root", "root");
        if(!$db) die("Error connecting to MySQL database.");
        mysqli_select_db("onlineform", $db);

As I try to connect to the database like shown above, I am getting the following error:

Warning: mysqli_connect(): [2002] No such file or directory (trying to connect 
via unix:///var/mysql/mysql.sock)  in - on line 3 Warning: mysqli_connect(): 
(HY000/2002): No such file or directory in - on line 3 Error connecting 
to MySQL database.

It's my first time using mysqli, I thought it would be a big improvement over mysql. I checked with MAMP if it's enabled and it is.

Any suggestions?

andrewsi
  • 10,807
  • 132
  • 35
  • 51
John Smith
  • 11
  • 3
  • Try [THIS](http://stackoverflow.com/questions/3968013/cakephp-no-such-file-or-directory-trying-to-connect-via-unix-var-mysql-mysq#answer-8926572). – Luigi Siri Jul 12 '13 at 13:38
  • 1
    Check [this][1]. It's a common problem. [1]: http://stackoverflow.com/questions/4219970/warning-mysql-connect-2002-no-such-file-or-directory-trying-to-connect-vi – ninjapro Jul 12 '13 at 13:42

1 Answers1

0

Check if your socket is correct. If not, override the default socket. I guess you have to try something like this in MAMP:

$link = mysql_connect(
  ':/Applications/MAMP/tmp/mysql/mysql.sock',
  'root',
  'root'
);
albin
  • 194
  • 3
  • 13
  • Warning: mysqli_query() expects at least 2 parameters, 1 given in - on line 10 Warning: mysqli_error() expects exactly 1 parameter, 0 given in - on line 12 Could not run query: < - I'm getting that error with your solution, but I think you have the right idea. How can I fix that though? – John Smith Jul 12 '13 at 14:09
  • Okay, you are still using localhost. First find the location of mysql.sock file on your server. Then map to that file in your mysql_connect() function. – albin Jul 12 '13 at 14:41
  • Also, did you enable php_mysqli extension in PHP.ini file? – albin Jul 12 '13 at 14:44