mysql访问被用户''@ localhost'拒绝访问数据库''

时间:2015-11-19 06:43:49

标签: php mysql

这是我目前在config.php中的代码:

mysql_connect("localhost","roof") or die(mysql_error());
mysql_select_db("tarclibrary") or die(mysql_error());

这是我在register.php中的当前代码:

require('config.php');

if(isset($_POST["submit"])){

//Perform the verification of the nation
$email1 =$_POST['email1'];
$email2 =$_POST['email2'];
$pass1 =$_POST['pass1'];
$pass2 =$_POST['pass2'];

if($email1 == $email2){
    if($pass1 == $pass2){
        //All good.carry on

        $fname = mysql_real_escape_string($_POST['fname']);
        $lname = mysql_real_escape_string($_POST['lname']);
        $uname = mysql_real_escape_string($_POST['uname']);
        $pass1 = mysql_real_escape_string($pass1);
        $pass2 = mysql_real_escape_string($pass2);
        $email1 = mysql_real_escape_string($email1); 
        $email2 = mysql_real_escape_string($email2);

        $pass1= md5($pass1);

        $sql = mysql_query("SELECT * FROM 'users' WHERE 'uname' = '$uname'");
        if(mysql_num_rows($sql) > 0){
            echo "Sorry, that user already exits";
            exit();
        }

        mysql_select_db('library') or die(mysql_error());

        mysql_query("INSERT INTO users (id, fname, lname, uname, pass, email) VALUES (NULL, '$fname', '$lname', '$uname'. '$pass1', '$email1')") or die (mysql_error());


    }else{
        echo "sorry,your passwords do not match.<br/>";
        exit();
    }
}else{
    echo "sorry your email's do not match<br/><br/>";
}
}else{

$form = <<<EOT

<h1><center>Register</center></h1>
<br>
<form action="register.php" method="post">
<table class="t1" align="center" width="600" height="400">
<tr>
   <th scope="row">First name</th>
   <td><input type="text" name="fname" required type="text"/></td>
</tr>
<tr>
   <th scope="row">Last name</th>
   <td><input type="text" name="lname" required type="text"/></td>
</tr>
<tr>
   <th scope="row">Username</th>
   <td><input type="text" name="uname" required type="text"/></td>
</tr>
<tr>
   <th scope="row">Password</th>
   <td><input type="password" name="pass1" required type="text" /></td>
</tr>
<tr>
   <th scope="row">Confirm Password</th>
   <td><input type="password" name="pass2" required type="text"/></td>
</tr>
  <tr>
   <th scope="row">Email</th>
   <td><input type="text" name="email1" required type="text"/></td>
</tr>
<tr>
   <th scope="row">Confirm Email</th>
   <td><input type="text" name="email2" required type="text"/></td>
  </tr>
</table>
<input id="register" type="submit" value="Register" name="submit"/>
</form>

EOT;

echo $form;

}

当我提交我的注册表时,输出是:

  

用户'''localhost'拒绝访问数据库'library'

有人帮忙,为什么会这样?

4 个答案:

答案 0 :(得分:5)

尝试连接以下

mysql_connect("localhost","root", '') or die(mysql_error())
  

MYSQL已弃用所以请使用mysqli

答案 1 :(得分:1)

使用mysqli

尝试以下操作
$mysqli = new mysqli("localhost", "root", "", "db_name");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
}

答案 2 :(得分:0)

试试这个。

mysql_connect("localhost","root", "") or die(mysql_error());
//replace third param with your actual password

不推荐使用mysql_connect ..或使用

$con = mysqli_connect("localhost","root","","tarclibrary");
// To Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

答案 3 :(得分:0)

您将按如下方式连接到数据库

     $link=mysql_connect("localhost", "root","password");
     mysql_select_db('tarclibrary', $link);
     $sql = "INSERT into users values('', '$fname', '$lname', '$uname'. '$pass1', '$email1')";
    $res=mysql_query($sql);