...

View Full Version : Using data from SQL DB in a Session Variable - syntax error



dacoder96
01-31-2010, 02:53 AM
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


$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...

$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.

sitNsmile
01-31-2010, 02:58 AM
If I see this correctly. You need


$myusername = "something";

PappaJohn
01-31-2010, 03:06 AM
Presumably, the username is a string. As such, it must be enclosed in single quotes in your query.

thekooliest
01-31-2010, 03:50 AM
$query = "SELECT skins FROM accounts WHERE username=$myusername";




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:


$query = "SELECT skins FROM accounts WHERE username='$myusername'";


If that doesn't work I'll help you debug the issue further,
Sam

dacoder96
01-31-2010, 07:14 AM
the $myusername variable exists...


$myusername = $_POST['username'];
$myusername = stripslashes($myusername);
$myusername = mysql_real_escape_string($myusername);

so how can I fix it??

edit;
ive done this...

$query = "SELECT skins FROM accounts WHERE username='$myusername'";
still getting an error

MattF
01-31-2010, 08:58 AM
Until pyschic abilities become the norm, posting a usable error message might be helpful. Change this line:



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


to:



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

dacoder96
01-31-2010, 10:55 AM
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'

include(skins/Array/pageHeader.php)
this is now my line of code

$query = "SELECT skin FROM accounts WHERE username='$myusername'";

thekooliest
01-31-2010, 03:12 PM
well anyway, its returning the value as 'Array' not '1'


$query = "SELECT skin FROM accounts WHERE username='$myusername'";

Try using:



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

dacoder96
02-01-2010, 08:54 AM
ahh finally! it works !
thank you everyone for your help! now that it works I can do sooo much more!!

MattF
02-01-2010, 11:47 PM
Change your code to the following. You're needlessly selecting information using the * selector.



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

dacoder96
02-02-2010, 09:26 AM
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

abduraooft
02-02-2010, 09:32 AM
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?

dacoder96
02-02-2010, 10:44 AM
pretty much this

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum