PHP无法连接到MySQL [英] PHP cant connect to MySQL
本文介绍了PHP无法连接到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$link = mysql_connect('localhost', $username, $password);
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
我收到此错误:
无法连接:用户'www-data'@'localhost'的访问被拒绝(使用密码:否)
Could not connect: Access denied for user 'www-data'@'localhost' (using password: NO)
为什么?
$username="root";
$password="root";
$database="test";
function Save($name)
{
$link = mysql_connect('localhost', $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO test (name)" .
"VALUES ('" . $name . "')";
mysql_query($query);
mysql_close();
}
推荐答案
根据您的编辑,该问题似乎是范围界定的问题.
From your edit, the issue appears to be a scoping one.
您正在尝试在函数内建立连接,而在该函数外定义了$username
,$password
和$database
变量.
You're attempting to make a connection inside a function where the $username
, $password
and $database
variables are defined outside that function.
我建议您阅读手册的变量范围部分,根据问题注释中@netcoder的建议,打开错误报告.
I suggest you read the Variable Scope section of the manual and turn up your error reporting as @netcoder suggests in the question comments.
这篇关于PHP无法连接到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文