-1

I am trying to connect to my MySQL server on a mac using PHP after starting the server using brew services start mysql After this I can see that the MySQL server has successfully started. However, when I try to access my webpage using:

<?php


/* Attempt to connect to MySQL database */
$link = mysqli_connect('localhost', 'root', 'password', 'databasename');
 
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>

The webpage start spinning continuously and doesn't load. I was wondering what's the issue as I can log into the root from the command line as well as see the tables. And the bre shows MySQL server running in the background.

EDIT I managed to print my error however, now I get an error:

Warning: mysqli_connect(): (HY000/2002): No such file or directory However, homebrew shows my MySQL server running.

ashes999
  • 1,234
  • 1
  • 12
  • 36
  • what errror do you get exactly and have you checked if the service is running – nbk Nov 21 '20 at 22:13
  • Yes I see the service running when I see the list of services on homebrew, however, I do not see any errors the windows loader is constantly spinning and my page doesn't come up. – ashes999 Nov 21 '20 at 22:16
  • that is your hole side, if yiou don't get an error the php code connected, try to run `select version()` and echo the output – nbk Nov 21 '20 at 22:18
  • @nbk I got the output printed however, i see an error that it cannot while I have connected. – ashes999 Nov 21 '20 at 22:22
  • then enable in in php.ini – nbk Nov 21 '20 at 22:25

2 Answers2

2

Try to change your host from localhost to 127.0.0.1

$link = mysqli_connect('127.0.0.1', 'root', 'password', 'databasename');
Andy Winarko
  • 645
  • 1
  • 9
  • 20
1
  1. You better make sure if mysqli extension is available in php.ini (If it is not enabled, go into the php.ini file and remove ; from the mysqli line.)

  2. If mysqlit is enabled, try running the code below and restart the server/terminal php task if anything changes

    cd /var mkdir mysql cd mysql ln -s /tmp/mysql.sock mysql.sock
    
  3. Check your credentials you give into the mysqli_connect

  4. If everything is okay, and you cant still connect try installing helpful programs like MAMP. They will create an Apache Server with Php and MySql server on local computer without a problem with full configuration

cangokceaslan
  • 467
  • 1
  • 5
  • 12