Page 1 of 1

problem with login script, one line, full code

Posted: Sun Jan 18, 2004 5:37 am
by malcolmboston
mysql_fetch_array problems

i am having serious problems, i believe the script i am writing is correct but it keeps on coming up with a mysql error regarding mysql_fetch_array

problem with if/else regarding header redirection

Code: Select all

<?php 
$host = localhost;
$username = malcolmboston;
$password = xxxxx;
$database = malcolmbostonDB;
$table = login;

$connection = mysql_connect($host, $username, $password) 
   or die (); 

$db = mysql_select_db("$database") 
   or die ();
$username = $_POST['username']; 
$password = $_POST['password']; 
$db_result = mysql_query("SELECT password FROM users WHERE username='$username'"); 
$db_password = mysql_fetch_array($db_result); 
$db_password = $db_password['password']; 
if (md5($password) == $db_password) 
{ 
	header("logged_in/index.html");
} 
else
{
	header("login_failed.html");
}
exit();
?>
this is the error message i get
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\FoxServ\www\TDN-New\Scripts\check_login.php on line 7
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at PHPDocument1:16) in <b>PHPDocument1</b> on line <b>24</b><br />
this is my preferred method of doing it however it wont let me due to the fact there are 2 'headers' is there anyway around this?
single header

Code: Select all

<?php 
$host = localhost;
$username = malcolmboston;
$password = xxxxxx;
$database = malcolmbostonDB;
$table = login;

$connection = mysql_connect($host, $username, $password) 
   or die (); 

$db = mysql_select_db("$database") 
   or die ();
$username = $_POST['username']; 
$password = $_POST['password']; 
$db_result = mysql_query("SELECT password FROM users WHERE username='$username'"); 
$db_password = mysql_fetch_array($db_result); 
$db_password = $db_password['password']; 
if (md5($password) == $db_password) 
{ 
    header("logged_in/index.html");
} 
else
{
	print 'Unfortunately that username and password doesnt exist';
}
exit();
?>
This is the error message i get
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\FoxServ\www\TDN-New\Scripts\check_login.php on line 7
i would prefer not to use this

Posted: Sun Jan 18, 2004 6:02 am
by vigge89
try replacing

Code: Select all

<?php
$db_result = mysql_query("SELECT password FROM users WHERE username='$username'");
$db_password = mysql_fetch_array($db_result); 
?>
with

Code: Select all

<?php
$db_query = "SELECT password FROM users WHERE username='$username'";
$db_password = mysql_fetch_array (mysql_query ($db_query)); 
?>

Posted: Sun Jan 18, 2004 7:02 am
by fastfingertips
You should make fetch array only if you have something in the result set, because if the username is wrong the result set is empty, so the code must look like:

Code: Select all

<?php
if(mysql_num_rows($db_result ) !=0)
{
     $db_password = mysql_fetch_array($db_result); 
    //Now follows the other things
}


?>