Forum Moderators: coopster

Message Too Old, No Replies

Help with mySQL Login Function

         

DuffMan

3:59 am on Dec 20, 2003 (gmt 0)

10+ Year Member



I'm trying to create a simple login script with a mySQL users database. Here's the code:

$result = mysql_query ("SELECT password FROM users WHERE username=$username",$link);
$row = mysql_fetch_array($result);
if ($_POST['password'] == $row['password']){
$loggedin = 1;
}

The error I'm getting says "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource". I'm probably just making a simple mistake but I can't find it.

Thanks in advance.

IanKelley

7:38 am on Dec 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Either the user in question does not exist or you're not actually connected to the database.

coopster

2:00 pm on Dec 20, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Duffman!

One way to troubleshoot problems like this is to create your query statement as a separate variable and exit [php.net] your code. This will halt all processing and output the $sql variable to your browser so you can see where the statement isn't looking right. I noticed you are not enclosing your password string in quotation marks. MySQL specifies that string and date values are specified as quoted strings.:


$sql= "SELECT password FROM users WHERE username='$username'";
//exit($sql); // uncomment this line to preview your statement.
$result = mysql_query ($sql,$link);