Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Jul 2009
    Location
    Most of the time - internet
    Posts
    85
    Thanks
    0
    Thanked 1 Time in 1 Post

    Using data from SQL DB in a Session Variable - syntax error

    Hi, thanks for opening the thread!

    I'm coding a blog site and for it to work I need to be able to get data from the database and use it. I need to be able to store information when a user log's in.
    This is how it works;
    - the user logs in and if the account exists then they are granted access and then some session variables are set.
    - each user has a personal choice of what skin (theme) they wish to use, there choice is stored in the database. I need to be able to use the stored variable in the database and use it as a session variable.
    This is the code I have so far... it doesn't seem to get the value

    PHP Code:
        $query "SELECT skins FROM accounts WHERE username=$myusername";
        
    $result mysql_query ($query) or die ('Cannot execute the query.');
        
    $userSkin mysql_fetch_array ($result);
        
    $_SESSION['skin'] = $userSkin
    When I log in it just says Cannot execute the query. I'm guessing its in this line somewhere...
    PHP Code:
    $query "SELECT skins FROM accounts WHERE username=$myusername"
    My column that the personal choice of the skin is store in is called 'skins'
    the table that it is stored in is called accounts.

    It is must appreciated if you could help me get it working. If anymore information is needed I will be glad to post it.

    Thanks.

  • #2
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    If I see this correctly. You need

    PHP Code:
    $myusername "something"

  • #3
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Presumably, the username is a string. As such, it must be enclosed in single quotes in your query.
    John

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by dacoder96 View Post
    PHP Code:
    $query "SELECT skins FROM accounts WHERE username=$myusername"
    Quote Originally Posted by PappaJohn View Post
    Presumably, the username is a string. As such, it must be enclosed in single quotes in your query.
    I agree with PappaJohn, your code should be:
    PHP Code:
    $query "SELECT skins FROM accounts WHERE username='$myusername'"
    If that doesn't work I'll help you debug the issue further,
    Sam

  • #5
    New Coder
    Join Date
    Jul 2009
    Location
    Most of the time - internet
    Posts
    85
    Thanks
    0
    Thanked 1 Time in 1 Post
    the $myusername variable exists...

    PHP Code:
    $myusername $_POST['username'];
    $myusername stripslashes($myusername);
    $myusername mysql_real_escape_string($myusername); 
    so how can I fix it??

    edit;
    ive done this...
    PHP Code:
        $query "SELECT skins FROM accounts WHERE username='$myusername'"
    still getting an error
    Last edited by dacoder96; 01-31-2010 at 06:17 AM. Reason: added more

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Until pyschic abilities become the norm, posting a usable error message might be helpful. Change this line:

    Code:
    $result = mysql_query ($query) or die ('Cannot execute the query.');
    to:

    Code:
    $result = mysql_query ($query) or die (mysql_error());

  • #7
    New Coder
    Join Date
    Jul 2009
    Location
    Most of the time - internet
    Posts
    85
    Thanks
    0
    Thanked 1 Time in 1 Post
    man, i never thought of that! I feel really dumb now =/

    ok, well ive done some playing around and this is what happens
    oh and btw, my column was called skin not skins, that was 1 of the errors, sorry
    well anyway, its returning the value as 'Array' not '1'
    PHP Code:
    include(skins/Array/pageHeader.php
    this is now my line of code
    PHP Code:
        $query "SELECT skin FROM accounts WHERE username='$myusername'"

  • #8
    New Coder
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by dacoder96 View Post
    well anyway, its returning the value as 'Array' not '1'

    PHP Code:
        $query "SELECT skin FROM accounts WHERE username='$myusername'"
    Try using:

    PHP Code:
    $query mysql_query("SELECT * FROM accounts WHERE username='$myusername'");
    while(
    $row mysql_fetch_array($query)){
    $userskin $row['skin'];


  • #9
    New Coder
    Join Date
    Jul 2009
    Location
    Most of the time - internet
    Posts
    85
    Thanks
    0
    Thanked 1 Time in 1 Post
    ahh finally! it works !
    thank you everyone for your help! now that it works I can do sooo much more!!

  • #10
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Change your code to the following. You're needlessly selecting information using the * selector.

    PHP Code:
    $query mysql_query("SELECT skin FROM accounts WHERE username='$myusername' LIMIT 1");
    $_SESSION['skin'] = mysql_fetch_result($query); 

  • #11
    New Coder
    Join Date
    Jul 2009
    Location
    Most of the time - internet
    Posts
    85
    Thanks
    0
    Thanked 1 Time in 1 Post
    thanks for the reply!
    i would like to use that one because it is shorter and just as good but mysql_fetch_result doesn't work. Ive tried other mysql_row_** things but they dont work.
    Which function would work?

    thanks

  • #12
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    i would like to use that one because it is shorter and just as good but mysql_fetch_result doesn't work.
    If that doesn't work, no other related functions would work. So, you need to find out the issue in your code. What's your current code?
    Last edited by abduraooft; 02-02-2010 at 08:34 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #13
    New Coder
    Join Date
    Jul 2009
    Location
    Most of the time - internet
    Posts
    85
    Thanks
    0
    Thanked 1 Time in 1 Post
    pretty much this
    PHP Code:
    if($count==1) {
        
    $_SESSION['blogUsername'] = $blogUsername;
        
    // skin selection
        
    $queryBlogUsername mysql_query("SELECT skin FROM accounts WHERE username='$blogUsername' LIMIT 1");
        
    $_SESSION['blogSkin'] = mysql_fetch_row($queryBlogUsername) or die(mysql_error());  
        
        
    //group selection
        
    $queryBlogGroup mysql_query("SELECT group FROM accounts WHERE username='$blogUsername' LIMIT 1");
        
    $_SESSION['blogGroup'] = mysql_fetch_row($queryBlogGroup); 

        
    $_SESSION['blogSkin'] = $blogSkin;
        
    $_SESSION['blogGroup'] = $blogGroup;
        
    $_SESSION['blogAccess'] = '1';
        
    //header('location: index.php');

    ive just changed the name of some variables, its pretty easy to tell which variable is which comparing to the old one


  •  

    Posting Permissions

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