php 用户 ''@'localhost' 访问被拒绝(使用密码:NO)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15015031/
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
Access denied for user ''@'localhost' (using password: NO)
提问by user1214840
Attempting to connect to localhost sql db using the following code (Not doing anything with query at this point just want the query to execute):
尝试使用以下代码连接到 localhost sql db(此时不对查询做任何事情,只想执行查询):
<?php
$host = "localhost";
$port = 3306;
$socket = "";
$user = "root";
$password = "Password1";
$dbname = "CIIP_WIKI";
$con = new mysqli($host, $user, $password, $dbname, $port);
if(!$con)
{
echo ("db connection failed!");
die ('Could not connect to the database server' . mysqli_connect_error());
}
else {
echo ("db connection established.");
echo ("<br/>");
}
$query = sprintf("SELECT first_name FROM actor WHERE actor_id='1'");
$result = mysql_query($query);
$con->close();
?>
I keep getting the following...
我不断收到以下...
Welcome
db connection established.
Warning: mysql_query(): Access denied for user ''@'localhost' (using password: NO) in C:\Program Files (x86)\EasyPHP-12.1\www\Cisco Wiki\index.php on line 31
Warning: mysql_query(): A link to the server could not be established in C:\Program Files (x86)\EasyPHP-12.1\www\Cisco Wiki\index.php on line 31
Why does this not work?
为什么这不起作用?
回答by Kermit
This is because you create a connection using mysqli_and then use mysql_to try to fetch your result. They are different API's.
这是因为您创建了一个连接mysqli_,然后使用它mysql_来尝试获取您的结果。它们是不同的 API。
<?php
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . mysqli_get_host_info($link) . "\n";
mysqli_close($link);
?>
回答by Vincent
This warning will be logged anytime you try to execute a query without a valid connection object being available.
每当您尝试在没有有效连接对象可用的情况下执行查询时,都会记录此警告。
In your case you thoughtyou had a valid connection object available, but you created it using mysqli while your query is being executed using mysql. Those are two different APIs and so the mysql_query function was looking for a mysql connection object and didn't find it.
在您的情况下,您认为您有一个有效的连接对象可用,但是您在使用 mysql 执行查询时使用 mysqli 创建了它。这是两个不同的 API,因此 mysql_query 函数正在寻找 mysql 连接对象,但没有找到。

