Wrox Programmer Forums
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 7th, 2003, 07:25 AM
Authorized User
 
Join Date: Jun 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default user levels

Think this is probably just something stupid that im doing wrong, but..

I have a administrator section on my site and am trying to set up two user levels, moderator and administrator. I have an idea of how to do it, im thinking search database for user level where username = $username (the one theyre logged in as)

Code:
include("includes/connect.php");


//start sql query
$sql=@"SELECT user_level FROM users WHERE username = '$username'";

$result=mysql_query($sql,$link_id);

if ($result =="administrator"){ //display stuff here }


The problem is if I print $result is just says "Array" rather than the search result. The query seems to be working ok, do I have to do something to the result befor I can check if it is equal to "administrator"?
 
Old July 7th, 2003, 02:38 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Remember that $result is a result identifier, not the actual text of your query. You still have to fetch the actual data from the result set before you play with it.

Read the documentation page online for details:
  http://www.php.net/mysql_query

Code:
$result = mysql_query($sql);

if($result != FALSE)
{
    $lvl = mysql_result($result, 0); // http://www.php.net/mysql_result
    if($lvl == "administrator")
    {
        // display stuff here
    }
}

Take care,

Nik
http://www.bigaction.org/
 
Old July 7th, 2003, 02:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One more thing:

Personally, I also think it's best to use a numeric id for your user levels. Add a new table, user_levels, to your database. The table will just be a unique ID field and a text description.

The user level stored in the users table will be the numeric ID, not the text. This is called "normalizing" your database, which in the long run increases efficiency and decreases storage space.

Take, for example, a users table with 5 administrators. Your database table takes up at least 13 bytes for each user to store the text "administrator" in the user levels, a total of 65 bytes. Compare that to the 5 bytes it would take to store a 1-byte integer that matches the numeric ID of the user_levels table.

This is how it'd all break down:

Code:
table: user_levels
id     desc
1      basic user
2      moderator
3      administrator
...

table: users
id     username         user_level_id
1      nikolai          1
2      dazednconfused   3
...
Now, your query will be something:

Code:
$query = "SELECT desc from user_levels
           INNER JOIN users ON
                 user_levels.id = users.user_level_id
             AND users.username = $username";
If you're not comfortable using JOINs, use this equivalent query:

Code:
$query = "SELECT user_levels.desc from users, user_levels
           WHERE users.username = $username
             AND users.user_level_id = user_levels.id";
Make sense?

There's lots of information and tutorials out there about efficient database design, normalization, joins, etc. Happy hunting!



Take care,

Nik
http://www.bigaction.org/
 
Old July 8th, 2003, 04:29 AM
Authorized User
 
Join Date: Jun 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, that was most helpfull, I hadnt really thought about doing it that way.





Similar Threads
Thread Thread Starter Forum Replies Last Post
levels for the small stars crmpicco Forum and Wrox.com Feedback 2 December 5th, 2006 05:33 AM
Re-Odering levels Brendan Bartley Access 5 November 16th, 2006 12:41 PM
Isolation Levels - Locking SQLScott SQL Server 2005 6 May 24th, 2006 11:59 AM
User access levels without using the mdw RobCarter Access 1 April 18th, 2006 08:11 PM
To tables combined to two levels jzac HTML Code Clinic 2 October 20th, 2004 04:20 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.