Page 1 of 1

mySQL + php problem

Posted: Tue Sep 23, 2003 2:14 pm
by freakishkid
hi all

i am trying to write this small piece of php that checks for a field called "admin" in a table called "users", and sees if that field is a 1 or a 0, and give an outcome based on that query.

this is what i have so far:

Code: Select all

$username = $_SESSION['username'];
$sqlQuery = "select admin from users where username = '$username'";
$result = mysql_query($sqlQuery);
if($result == 1)
{
    // The query is ok, now check for admin
    $row = mysql_fetch_row($result);
    $admin = $row[0];
    if($admin != 1)
    {
        // User is NO ADMIN
        echo '<a href="apply.php">apply</a><br>';
    } else {
        // User is ADMIN
        echo '<a href="template.php">template</a><br>';    
    } 
} else {
    echo "No user $username found";
}

?>
my problem is, even though i know there IS a username field called "freakishkid", whenever i am logged in with that user, the script returns the second else ("No user freakishkid found"). could anyone assist me with this?

i guess that it is something simple, but i am new to php so cannot see it.

thanks in advance,
fk

Posted: Tue Sep 23, 2003 3:32 pm
by freakishkid
silly me...

ok this has just become a much simple question.

how do i change this line

Code: Select all

$sqlQuery = "select admin from users where username = '$username'";
so that its actually pointing to a table called "users" within a database called "freakish_login"?

yes, i'm that clever :wink:

Posted: Tue Sep 23, 2003 3:43 pm
by freakishkid
no hang on that isn't the issue either, because i have a seperate script earlier called db_connect.php that connects to the freakish_login database, so surely that reference to the "users" table should be understood already?

Posted: Tue Sep 23, 2003 6:59 pm
by Unipus

Code: Select all

mysql_select_db("freakish");
$sqlQuery = "select admin from users where username = '$username'"; 
// possibly you'd want to switch back your selected DB here

Posted: Wed Sep 24, 2003 2:22 pm
by microthick
You'll probably wanna use mysql_num_rows() to see if one row is returned rather than doing that equality check.

Code: Select all

$result = mysql_query($sqlQuery); 
$num_rows = mysql_num_rows($result);
if($num_rows == 1)  &#123;
...

Posted: Wed Sep 24, 2003 3:51 pm
by brewmiser
Your line of code:

Code: Select all

$sqlQuery = "select admin from users where username = '$username'";
Your line of code should be:

Code: Select all

$sqlQuery = "select admin from users where username = '".$username."'";
Also use microthick's code after this line.