user695577
user695577

Reputation: 7

select based on session

How do I select a value from a database where username is based on the session?

This is what I have so far:

$query = mysql_query ("select id from CUSTOMER where username = .$_SESSION['username'] ");

Upvotes: 0

Views: 13422

Answers (3)

Ondrej Slinták
Ondrej Slinták

Reputation: 31910

Session variable in your query wasn't parsed properly. You could fix it using curly bracers syntax:

$query = mysql_query( "select id from CUSTOMER where username = '{$_SESSION[ "username" ]}'" );

or concatenate it using dot operator:

$query = mysql_query ( "select id from CUSTOMER where username = '" . $_SESSION[ "username" ] . "'" );

You can find more about parsing strings in PHP manual.

Upvotes: 1

Mikecito
Mikecito

Reputation: 2063

$query = mysql_query("select id from CUSTOMER where username = '".$_SESSION['username']."'");

Upvotes: 1

Vishwanath Dalvi
Vishwanath Dalvi

Reputation: 36611

If username is in session cookie then grab the username like this

$username = $_SESSION['username'];
$escuname = mysql_real_escape_string($username);
$query = mysql_query("select id from CUSTOMER where username = '".$escuname."' LIMIT 1"); 

Upvotes: 3

Related Questions