php 警告: mysqli_connect(): (HY000/1049): 仅 mac 终端中的未知数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45979819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:10:24  来源:igfitidea点击:

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

phpmysqlapachexampp

提问by Maulik Patel

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

在 mac 中,我想创建 cronjob,但是当我使用数据库运行 php 文件时,我在终端中遇到错误。

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

And In the browser run perfect.

并在浏览器中完美运行。

采纳答案by dingezzz

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

你的 PHP 环境和你的浏览器 PHP 一样吗?命令行 php.ini 可以不同于,例如,您的 XAMP 或其他已安装的网络服务器

I would recommend checking this first

我建议先检查这个

for me this did the trick

对我来说,这成功了

edit your .bash_profilefile like

编辑您的.bash_profile文件,例如

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

edit the path to your PHP bin from your XAMP

从 XAMP 编辑 PHP bin 的路径

回答by kamyar

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:

检查 mysql 运行的端口。在许多情况下,mysql 在端口 3306 中运行,但在我的 mysql 中,它在端口 3308 中运行,我添加了以下内容:

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

回答by CoderSoup

In my case I got similar error

就我而言,我遇到了类似的错误

Warning: mysqli_connect(): (HY000/1049): Unknown database 'login.db' in D:\xampp\htdocs\login\functions\db.php on line 2 so i change [$con=mysqli_connect('localhost', 'root', '', 'login.db');] into [$con=mysqli_connect('localhost', 'root', '', 'login');]

警告:mysqli_connect(): (HY000/1049): Unknown database 'login.db' in D:\xampp\htdocs\login\functions\db.php 在第 2 行所以我改变 [$con=mysqli_connect('localhost', 'root', '', ' login.db');] 进入 [$con=mysqli_connect('localhost', 'root', '', ' login');]

Ii changed 'login.db' to 'login' and error disappear

ii 将 ' login.db'更改为 ' login' 并且错误消失

回答by Marko Perendio

If you have this code:

如果你有这个代码:

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

Try with creating new database called myDBin localhost/phpmyadmin.

尝试创建一个名为新的数据库myDBlocalhost/phpmyadmin

回答by Mabro DoubleC

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";

回答by user2811

For me, the problem was the "Port number" (on Step 3 of OpenCart 3.0.3.2 installation)which was set as 3306 but in MySQL, it was showing as 3308so I just changed the same and all went good.

对我来说,问题是“端口号” (在 OpenCart 3.0.3.2 安装的第 3 步中)设置为 3306,但在 MySQL 中,它显示为3308,所以我只是更改了相同的值,一切顺利。

enter image description here

在此处输入图片说明