Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 7th, 2003, 07:25 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
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"?
Reply With Quote
  #2 (permalink)  
Old July 7th, 2003, 02:38 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
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/
Reply With Quote
  #3 (permalink)  
Old July 7th, 2003, 02:49 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
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/
Reply With Quote
  #4 (permalink)  
Old July 8th, 2003, 04:29 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:54 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.