Warning: mysqli_connect(): (HY000/1049): Unknown database in mac terminal only

48,185

Solution 1

Is your PHP environment the same as your browsers PHP? command line php.ini can differ from, for example, your XAMP or other installed webserver

I would recommend checking this first

for me this did the trick

edit your .bash_profile file like

export PATH=/Applications/MAMP/bin/php/php7.1.1/bin:$PATH

edit the path to your PHP bin from your XAMP

Solution 2

check your port that mysql run . in many cases mysql run in port 3306 but in my mysql it run in port 3308 and I add this:

$conn = new mysqli("localhost", "root", "", "myDB","3308");

Solution 3

I had a similar problem. My code was like this:

$conn=mysqli_connect($servername,$username,$password,$dB) or die("connection failed");
echo "connection success";

Problem: I did not indicate the port number. Solution: I just included the port number, like below:

$conn=mysqli_connect($servername,$username,$password,$db,"3308") or die("connection failed");
echo "connection success";

Solution 4

If you have this code:

$conn = new mysqli("localhost", "root", "", "myDB");

Try with creating new database called myDB in localhost/phpmyadmin.

Share:
48,185
Maulik Patel
Author by

Maulik Patel

I am Computer Engineer and working in various technologies like Node Js, Angular and Mobile technologies like React native, Android, iPhone. Now a days willing to work in IOT and AI related technologies.

Updated on April 03, 2020

Comments

  • Maulik Patel
    Maulik Patel about 4 years

    in mac i want to create cronjob but when i run the php file with database i am getting error in terminal.

    Warning: mysqli_connect(): (HY000/1049): Unknown database 
    

    And In the browser run perfect.