mySQL + php problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
freakishkid
Forum Newbie
Posts: 3
Joined: Tue Sep 23, 2003 2:14 pm
Contact:

mySQL + php problem

Post 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
freakishkid
Forum Newbie
Posts: 3
Joined: Tue Sep 23, 2003 2:14 pm
Contact:

Post 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:
freakishkid
Forum Newbie
Posts: 3
Joined: Tue Sep 23, 2003 2:14 pm
Contact:

Post 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?
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post 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
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post 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;
...
User avatar
brewmiser
Forum Commoner
Posts: 74
Joined: Mon Aug 18, 2003 12:50 pm
Location: Dallas, TEXAS

Post 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.
Post Reply