Sessions with SQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
markkam
Forum Newbie
Posts: 3
Joined: Wed Aug 28, 2002 12:56 pm

Sessions with SQL

Post by markkam »

Any Idea why this code wont work?
Code: wrote: $sql="SELECT * FROM users WHERE username=$_SESSION['username']";
$result=db_query($sql);
if (db_result($result,0,'is_admin') == '1') {
include("templates/adminmenu.inc");
}
I get the error

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

well my first thought would be #1 username probably isn't a numeric only field so you would need quotes ' ' around your username variable

try this

Code: Select all

$username = $_SESSIONї'username']"; 

$sql="SELECT * FROM users WHERE username = '$username'"
markkam
Forum Newbie
Posts: 3
Joined: Wed Aug 28, 2002 12:56 pm

Post by markkam »

Im pulling username from session data, not from a header or the like, which is how I am passing variables.

I need to be able to do it with the SESSION data for username.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

thats what the $_SESSION['yoursessionvariable'] does

http://www.php.net/manual/en/ref.session.php


from the php.net manual:

Code: Select all

Use of $_SESSION (or $HTTP_SESSION_VARS with PHP 4.0.6 or less) is recommended for security and code readablity. With $_SESSION or $HTTP_SESSION_VARS, there is no need to use session_register()/session_unregister()/session_is_registered() functions. Users can access session variable like a normal variable
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you may avoid the assignment

Code: Select all

$username = $_SESSIONї'username']";
if you use this syntax

Code: Select all

$sql="SELECT * FROM users WHERE username='{$_SESSIONї'username']}'";
only if you like to ;)
markkam
Forum Newbie
Posts: 3
Joined: Wed Aug 28, 2002 12:56 pm

Post by markkam »

volka wrote:you may avoid the assignment

Code: Select all

$username = $_SESSIONї'username']";
if you use this syntax

Code: Select all

$sql="SELECT * FROM users WHERE username={$_SESSIONї'username']}";
only if you like to ;)
Thank you!!! I've been beating myself over the head with a copy of windows trying to figure it out. :P
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

windows installation cd or the book "just the bugs"? ;)
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

OT


Volka wrote...
windows installation cd or the book "just the bugs"?
Thats either a lot of CDs or a one heavy book!
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Even more OT
Would such a book be liftable? I'ld be worried about it unbalancing the gravitation forces in our solar system....
Post Reply