0

My column type is SMALLINT and name is user_level. Here is the code I am using:

<?php
mysql_connect("localhost", "root", "*********");
mysql_select_db("3591_other");
$result = mysql_query("SELECT * FROM rivase_f_users WHERE user_name = '$email' AND    user_pass=sha1('$passwd')");
if (!$result) {
    echo mysql_error();
}
//snip
else if (mysql_fetch_assoc($result)) {
    $row=mysql_fetch_assoc($result);
    $_SESSION['username']=$email;
    if ($row['user_level']==1) {$_SESSION['usertype']='leader';}
    header("Location: index.php");
    die();
}
?>

$row['user_level'] looks like to be null, but phpmyadmin says it is 1. I tried echoing it with commenting the header-location row, it did not say anything. What am I doing wrong?

1
  • Again someone who use mysql_* functions. Btw WHERE user_name = '$email' Did you read this???? Commented Sep 13, 2012 at 7:37

4 Answers 4

2

try this,

else if ($row = mysql_fetch_assoc($result)) {
    $_SESSION['username']=$email;
    if ($row['user_level']==1) {$_SESSION['usertype']='leader';}
    header("Location: index.php");
    die();
}
Sign up to request clarification or add additional context in comments.

Comments

2

What you're trying to accomplish is unclear to me but you are discarding the first result:

else if (mysql_fetch_assoc($result)) {
    //   ^^^^^^^^^^^^^^^^^ Read and discard
    $row=mysql_fetch_assoc($result);
    //   ^^^^^^^^^^^^^^^^^ Read next row

Comments

1

You are experiencing a logical if-else bug.

You are reading the value of next mysql_fetch_assoc() after testing the output result of the first call in the else condition, but you wanted to read the value from first returned result.

Comments

1

Based on your code try this

<?php
mysql_connect("localhost", "root", "*********");
mysql_select_db("3591_other");
$result = mysql_query("SELECT * FROM rivase_f_users WHERE user_name = '$email' AND    user_pass=sha1('$passwd')");
if (!$result) {
    echo mysql_error();
}
//snip
else if ($row=mysql_fetch_assoc($result)) {
    $_SESSION['username']=$email;
    if ($row['user_level']==1) {$_SESSION['usertype']='leader';}
    header("Location: index.php");
    die();
}
?>

I've changed the else if check to save the row when doing the comparison.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.