简体   繁体   English

mysqli_connect 没有连接到本地主机

[英]mysqli_connect not connecting to localhost

As title suggests, I'm getting the following error when using mysqli_connect:正如标题所暗示的,我在使用 mysqli_connect 时收到以下错误:

Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known in /Users/username/Sites/test.php on linez 2警告:mysqli_connect(): php_network_getaddresses: getaddrinfo failed: nodename 或 servname 提供,或在 linez 2 上的 /Users/username/Sites/test.php 中未知

Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known in /Users/username/Sites/test.php on line 2 Could not connect:警告:mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: nodename or servname provided, or not known in /Users/username/Sites/test.php on line 2 无法连接:

<?php
$link = mysqli_connect(‘localhost’, ‘username’, ‘password’);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

My firewall is turned off and apache2 is turned on, I am able to view localhost/Username/Sites/test.php as well as logging into mysql using localhost.我的防火墙已关闭,apache2 已打开,我可以查看 localhost/Username/Sites/test.php 以及使用 localhost 登录 mysql。

from where you bring those quotes ?你从哪里带来这些报价? you should use single quotes.你应该使用单引号。

try this:尝试这个:

    $link = mysqli_connect('localhost', 'username', 'password'); //you forgot the database

and why you mix between mysqli and mysql ?为什么你在 mysqli 和 mysql 之间混合?

Like that you connect to database:就像你连接到数据库:

 $link = mysqli_connect("localhost","username","password","database") or die("Error " . mysqli_error($link));

please read good the documentation here http://se2.php.net/mysqli_select_db请阅读这里的文档http://se2.php.net/mysqli_select_db

如果您使用的是 OS X 中内置的 textedit 应用程序,则从 TextEdit 的首选项窗格中禁用/取消选中名为“Smart Quotes”的功能,然后您将必须替换代码中的所有现有引号。

I have encountered this problem as well but none of the answers helped.我也遇到过这个问题,但没有一个答案有帮助。

Despite what many answers say, the database name is not mandatory as a 4th parameter, as per the documentation: https://www.php.net/manual/en/mysqli.construct.php - all the parameters are optional.尽管有很多答案,但根据文档: https : //www.php.net/manual/en/mysqli.construct.php - 所有参数都是可选的,数据库名称不是强制作为第四个参数。

For me what solved it was to double check the database host as in my code it is dynamic and it was picking up wrongly the distant database host instead of localhost.对我来说,解决它的是仔细检查数据库主机,因为在我的代码中它是动态的,并且它错误地选择了远程数据库主机而不是本地主机。

$mysqli = IS_LOCAL ? 
    new mysqli($settings->localDBhost, $settings->localUser, $settings->localPassword)
    : new mysqli($settings->DBhost, $settings->DBuser, $settings->DBpassword);

(After I created a virtual host, I had to add it to the IS_LOCAL constant's condition) (创建虚拟主机后,必须将其添加到IS_LOCAL常量的条件中)

$link = mysqli_connect(‘localhost’, ‘username’, ‘password’);

应该

$link = mysqli_connect(‘localhost’, ‘username’, ‘password’, 'yourdatabase');

Try:尝试:

$dbc = mysqli_connect('localhost', 'username', 'password', 'phonehome','3306','/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock') OR die('Could not connect to database. Verify connection.php settings and SQL DB is running. Error: '.mysqli_connect_error());

Which is defining your Hostname, port, username, password, db and mySQL socket, it worked for me.它定义了您的主机名、端口、用户名、密码、db 和 mySQL 套接字,它对我有用。

For some reason I can't remember (I think I read it somewhere else on SO, actually), when you connect to 'localhost' using mysqli(), you actually have to specify what port number you're connecting on as well:由于某种原因,我不记得(我想我在其他地方读过它,实际上),当您使用 mysqli() 连接到'localhost' ,您实际上还必须指定要连接的端口号:

$dbconn = new mysqli('localhost:3306', /*your username*/, /*your password*/, /*your database name*/);

3306 is, if I remember correctly, the default port, but even so you need to specify that in this case - literally only when you try to connect on localhost, but for some reason that's how it is. 3306 是,如果我没记错的话,是默认端口,但即便如此,在这种情况下您也需要指定它 - 实际上只有当您尝试连接本地主机时,但出于某种原因,它就是这样。

Also - I noticed that in your original code your quotation marks in your mysqli_connect(...) line look different from those in your other code lines.另外 - 我注意到在您的原始代码中,您的mysqli_connect(...)行中的引号看起来与其他代码行中的引号不同。 This may just be something that happened in stack overflow when you wrote out your question, but if it's not and those different quotation marks are in your code as well, then you may need to change them to the same ones as in your later code.这可能只是在您写出问题时在堆栈溢出中发生的事情,但如果不是并且那些不同的引号也在您的代码中,那么您可能需要将它们更改为与您以后的代码中相同的引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM