我正在尝试使用xampp php myAdmin连接mysql。mysql_connect():访问拒绝用户'root'@'localhost'(使用密码:否)
用户root
默认情况下是在MySQL数据库中。
这里是我的代码:
<html>
<body>
<?php
[email protected]_connect("localhost", "root", '');
if (!$dbCon)
{
echo('<p> '.mysql_error().'</p>');
exit();
}
else
{
if([email protected]_select_db("crs",$dbCon))
{
die("<p>".mysql_error()."</p>");
}
}
$qry="Create Table Courses(cCode VarChar(5) Not NULL Primary Key, cTitle VarChar(50) Not NULL, CH INT)";
if(@mysql_query($qry))
{
echo('<p>Courses Table has been created</p>');
}
else
{
echo('<p>Courses Table can not be created due to' .mysql_error(). '</p>');
}
$qry="Insert INTO Courses Values('C001','Intro to computing',4)";
if (@mysql_query($qry))
{
echo('<p>C001 is inserted </p>');
}
else
{
echo('<p>Courses Table cannot be created due to'.mysql_error().'</p>');
}
$qry="Insert INTO Courses Values ('C002', 'Programming Fundamentals',4), ('C003', 'Object Oriented Programming',4), ('C004', 'Operating Systems',4), ('C005', 'Data Structures and Algorithms',4)";
if(@mysql_query($qry))
{
echo('<p>4 Rows have been inserted</p>');
}
else
echo('<p>Multiple Rows cant be inserted due to'.mysql_error().'</p>')
?>
</body>
</html>
您的代码没有任何问题。然而,mysql已经建立了root用户需要密码从本地主机连接,你没有去你的电话mysql_connect。如果没有以管理员用户身份进行连接的方式,或者没有人记住他们如何设置mysql,则无法真正帮助您。 –