可能重复:
PHP - MySQL access denied error - Works in other programs
我正在尝试创建一个登录进程,在我的phpmyadmin中连接我的AdminLogin2.php和admin2表,但它表示拒绝访问。我不知道该怎么做才能解决它。希望有人可以帮助我。在这里我的代码,谢谢。
`<?php
$host="localhost";
$username="root";
$password="";
$database="finalproject";
$table="admin2";
$AdminID=$_POST['Field1'];
$Password=$_POST['Field3'];
mysql_connect("$host","$password") or die (mysql_error());
#echo"connected";
mysql_select_db("$database") or die (mysql_error());
#echo"database found";
// To protect MySQL injection (more detail about MySQL injection)
$AdminID = stripslashes($AdminID);
$Password = stripslashes($Password);
$AdminID = mysql_real_escape_string($AdminID);
$Password = mysql_real_escape_string($Password);
$sql="SELECT * FROM $admin2 WHERE username='$AdminID' and password='$Password'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:AdminForm.php");
}
else {
echo "Wrong Username or Password";
}
?>
`
答案 0 :(得分:7)
mysql_connect("$host","$password")
它应该像
mysql_connect("$host","$username","$password")
这就是为什么你从数据库的空用户名中得到错误