Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: php error sql if

  1. #1
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default php error sql if

    I have added in some code which checks if the user is an admin but I'm getting an error

    PHP Code:
    $sql mysql_query ("SELECT * FROM users") or die(mysql_error());; 
    $gordon mysql_fetch_array ($sql);
    if (
    $row['level']= "9"){
    echo 
    "Admin"

    ( ! ) Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\Canberra Amatuer Productions\echo time.php on line 103

    Any help?

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    PHP Code:
    echo "Admin"
    Should be

    Code:
    echo "Admin";}
    Last edited by james438; 08-14-2011 at 12:18 PM. Reason: added formatting
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Thanks, I changed what you said but it's still coming up with the same error. Any help?

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    not a solution, but
    Code:
    if ($row['level']= "9"){
    should be
    Code:
    if ($gordon['level']== "9"){
    In your code $row['level'] will always be true.

    Are you sure you are posting line 103 from your time.php file?
    Last edited by james438; 08-14-2011 at 12:33 PM.
    To choose the lesser of two evils is still to choose evil. My personal site

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by keyboard1333 View Post
    PHP Code:
    $sql mysql_query ("SELECT * FROM users") or die(mysql_error());; 
    $gordon mysql_fetch_array ($sql);
    if (
    $row['level']= "9"){
    echo 
    "Admin"
    ( ! ) Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\Canberra Amatuer Productions\echo time.php on line 103
    In addition, you have an extra semicolon after your first line above. It should look like this:
    PHP Code:
    $sql mysql_query("SELECT * FROM users") or die(mysql_error());
    $gordon mysql_fetch_array($sql);
    if (
    $row['level'] == "9"){ echo "Admin"; } 
    If you're still getting an error, then it is occurring somewhere else.
    Remember that the error is reported at the line where PHP can no longer recover; which is not necessarily (in fact, usually isn't) where the actual mistake is.

    However, you're still only checking the first row. If the first user in your DB is an admin, this will always print "Admin"; if not, then it will never do anything.

    The query is also very heavy (you're retrieving all columns from all records but only using one column from one record). If you get hundreds of users (or thousands) and you're doing this check every time a certain page (or any page) is loaded, it will be slow. You should only select what you need in your query. You could even do the entire check via mysql, and not have to process any of it in php.

    If you want to know if a particular user is an admin, try something more like this:
    PHP Code:
    $userid ''// user id to check (whatever unique column you use)

    $SQL "SELECT `id` FROM `users` WHERE `id`='$userid' AND `level`='9' LIMIT 1";
    // this way, you only get a match if the user *also* has level=9.
    // if you get no results, then you know that the user is _not_ level=9.

    if( mysql_num_rowsmysql_query($SQL) ) === ){ print "User $userid is an Admin"; }
    else{ print 
    "User $userid is a loser."; } 
    Last edited by traq; 08-15-2011 at 01:36 AM. Reason: correction: db col name is "level", not "type". sorry : )

  6. #6
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    The unique column I am using to see if they are an admin is called level. If it is set to 1 they are a normal user if it is set to 9 they are an admin. I am not very good at sql so could you please just show me how to check that?

    Also in your code Traq, what should $userid equal?

  7. #7
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Where are you getting $row from if the array that is being fetched is stored in $gordon?

    $row should be replaced with $gordon. You also need two equal signs in a comparison operator (this is true of any OOP language).

    That syntax error you're gettting is because you don't have a semicolon after echo "Admin".

    Also, as Adrian said:
    Quote Originally Posted by traq View Post
    If you want to know if a particular user is an admin, try something more like this:
    Code:
    "SELECT `id` FROM `users` WHERE `id`='$userid' AND `type`='9' LIMIT 1";
    I suggest you do that too. Or if you're looping through all of the users, you need to use a loop.

    Anyway, try this:
    PHP Code:
    $sql mysql_query ("SELECT * FROM users") or die(mysql_error());

    $gordon mysql_fetch_array ($sql);

    if (
    $gordon['level'] == "9"){
    echo 
    "Admin";

    - Josh

  8. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by keyboard1333 View Post
    The unique column I am using to see if they are an admin is called level. If it is set to 1 they are a normal user if it is set to 9 they are an admin. I am not very good at sql so could you please just show me how to check that?

    Also in your code Traq, what should $userid equal?
    From what I see in your code, level is not unique. There might be only one level=9 user, but any number of users could have level=1, correct?

    What column do you have with a unique value for each record (e.g., your primary/unique key: id, user_id, username, etc.)?

    In my code example, $userid would hold the unique id of the user you are trying to check (and the query, of course, would use the appropriate col name). You might get that value from a login form, for example, <input name="username">, or you might get it from a user function, like get_user_id($_POST['username']). Make sure you sanitize the value before you use it in your query.

    Edit: sorry for the confusion - I meant to use your column name level in my post above, not type. I fixed it.
    Last edited by traq; 08-15-2011 at 01:43 AM.

  9. #9
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    I'm a bit confused. If I want to find out if the user that is logged in is an admin should it be like - select from user where username == the login cookie and level == 9?

    Could someone please just explain to me really simply what I have to do. Also Traq there can be more than one user who's level == 9.
    Also if I wanted to use the code with $userid I would have to find out what the user who is logged in's id is. so would the code look like - select id where username== the logincookie?

  10. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I don't know how your login system is set up, so I can't answer that question. If the username is unique (only one user can have any given username), then yes, you can use that.

    PHP Code:
    $username ''// <--- put username here
    $SQL "SELECT `id` FROM `users` WHERE `username`='$username' AND `level`='9'";
    if( 
    mysql_num_rowsmysql_query$SQL ) ) === ){
       
    /* $username _is_ an admin.
           Do Admin-related stuff.     */
    }else{  
       
    /* $username is _not_ an admin.
          He can't do admin-related stuff. */


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •