Login Script PHP

I have a little problem here i have created a blog and users are able to register but they can’t login in. The thing is the login form displays normally but when my users submit the form they get the following error

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in login.php on line 36

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in login.php on line 39

My login page script is:


//start session

$host = 'localhost'; // location of server.
$database = 'blog'; // database name
$user = 'login'; // database user
$pw = 'blahblah'; // db password
$conn = mysql_connect($host, $user, $pw) or die("Cannot connect to MySQL.");
$dbc = mysql_select_db($database, $conn) or die("Cannot connect to database.");
if($_POST["submit"]) {
$errors = array(); // array to handle error messages

if(!empty($_POST["username"])) {
	$login = mysql_real_escape_string(strip_tags($_POST["username"]));
else {
	$errors = "Please enter your login name!";

	$password = mysql_real_escape_string(strip_tags($_POST["passwd"]));
else {
	$errors = "Please enter your password!";

   //No errors. retrieve data from the database
   $query = "SELECT id, login, password FROM user_logins WHERE login='".addslashes($login)."'
    && password='".addslashes($password)."'";
   $result = mysql_query($dbc, $query);

 //If data found, fetch it and store it in a session
 if(mysql_num_rows($result)== 1) {
    #set session variable
    $_SESSION['id'] = $id;
    $_SESSION['login'] = $login;
    echo "Welcome <strong>.".$login.".</strong>!
    <a href='profile.php?view=$login'>click here</a> 
     to go to your profile or <a href='logout.php'>click here</a> to log out.";

} // close the if statement
 // now display the login form.

echo <<<_EOM
<p><form method="post" action="login.php">
     <td colspan="2"><b>Members log in area:</b></td>
     <td><input type="text" name="username" maxlength="12" /></td></tr>
     <td><input type="password" name="passwd" maxlength="42" /></td></tr>
     <td colspan="2" align="center">
     <input type="submit" name="submit" value="Log in"/></td></tr>
     <td colspan="2">Not a member? <a href="join.php"> Click here</a> to join</td>


Anyone able to figure out what I am doing wrong?

there’s a syntax and logic error in your [fphp]mysql_query[/fphp]

$result = mysql_query($dbc, $query);

Try this:

$result = mysql_query($query, $dbc);

OR this:

$result = mysql_query($query);

You don’t need to specify the $dbc. It’s optional. But both will work.

I realize that. how do I solve the problem… Any suggestions.

Ok let me try that. I’ll let you know if it worked out. Thanks

The link tells you how to solve the problem.

What part of the explanation in the link don’t you understand?

using $dbc is not correct in this situation even if you wanted to use it.

Thanks @mmarif4u, I managed to find out what was wrong. It now works.

You need either

$result = mysql_query($query, $conn);


$result = mysql_query($query);

Yup, you are right, i did not look into it. Mistake there. Thought its the connection variable.