I've spent quite a long time tryin开发者_如何学运维g to figure out why this block of code is not working. After logging in (on an html page that uses post to send the data to this php document) it says: "cannot select the database!!" (without quotes). Please help! Thank you!
<?php
$host="localhost";
$username="root";
$password="";
$db_name="firstTestLogins";
$tbl_name="members";
mysql_connect("$host", "$username, $password") or die("cannot connect to the database!!");
mysql_select_db("firstTestLogins") or die("cannot select the database!!");
$username=$_POST['username'];
$password=$_POST['password'];
$username=stripslashes($username);
$password=stripslashes($password);
$username=mysql_real_escape_string($username);
$password=mysql_real_escape_string($password);
$password = md5($password);
echo "This is a debug statement. User = $username and password = $password <br><br><br>";
if(!($sql="SELECT * FROM $tbl_name WHERE username = '$username' and password = '$password")) {die(mysql_error());}
$result = mysql_query($sql);
$test3 = mysql_num_rows(/*$result*/mysql_query($sql));
echo "$test3";
if(mysql_num_rows(/*$result*/mysql_query($sql))) { //should be true because only one row should match the user and pass
$_SESSION['username'] =1;
$_SESSION['password'] =1;
header("location:login_success.php");
}
else {
echo "The incorrect username or password was inserted";
}
?>
This is wrong:
mysql_connect("$host", "$username, $password");
it needs to be
mysql_connect($host, $username, $password);
you will see the exact problem by outputting mysql_error()
.
mysql_select_db("firstTestLogins")
or die("cannot select the database! ".mysql_error());
please change the code from:
mysql_select_db("firstTestLogins") or die("cannot select the database!!");
to:
mysql_select_db("firstTestLogins") or die(mysql_error());
This will help you to figure out why MYSQL cannot select the DB
In addition to the comments about selecting the database:
if(!($sql="SELECT * FROM $tbl_name WHERE username = '$username' and password = '$password")) {die(mysql_error());}
Silly doing a test with a mysql_error if the $sql string isn't being set... this won't generate a mysql error
You're also missing a closing ' in your $sql
$sql="SELECT * FROM $tbl_name WHERE username = '$username' and password = '$password'";
$result = mysql_query($sql);
if(!$result)
die(mysql_error());
Only thing I can think of is that the firstTestLogins
database either doesn't exist on the database or is not accessible by the username/password combination you're using.
精彩评论