Jump to content

use variables in mysql query


sdaniels

Recommended Posts

here is my code

[code]
$result = mysql_query("SELECT * FROM security_menu_main WHERE sec_level <= '$_SESSION[seclevel]' AND isactive = 'y'");
[/code]

the query is ignoring the "sec_level <= $_SESSION[seclevel]" and just checking if isactive is == y

what am I doing wrong?
Link to comment
Share on other sites

A tip is to echo out your query string, so you can see what's going on.


[code]
$query = "SELECT * FROM security_menu_main WHERE sec_level <= '$_SESSION[seclevel]' AND isactive = 'y'";
echo "original query: " . $query;

$query = "SELECT * FROM security_menu_main WHERE sec_level <= '" . $_SESSION['seclevel'] . "' AND isactive = 'y'";
echo "new query: " . $query;

$result = mysql_query($query);
[/code]
Link to comment
Share on other sites

hmm... maybe your problem is that $_SESSION variables are accessed using the following syntax:
$_SESSION['seclevel']


and in your query string, you're accessing the value using:
$_SESSION[seclevel]


maybe you're just missing the single quotes?

i've also read about using the tickmarks (`) around table and field names. could this be what's missing?
Link to comment
Share on other sites

[!--quoteo(post=363894:date=Apr 12 2006, 12:14 AM:name=hadoob024)--][div class=\'quotetop\']QUOTE(hadoob024 @ Apr 12 2006, 12:14 AM) [snapback]363894[/snapback][/div][div class=\'quotemain\'][!--quotec--]
hmm... maybe your problem is that $_SESSION variables are accessed using the following syntax:
$_SESSION['seclevel']
and in your query string, you're accessing the value using:
$_SESSION[seclevel]
maybe you're just missing the single quotes?

i've also read about using the tickmarks (`) around table and field names. could this be what's missing?
[/quote]

thanks for the help, it turens out that i have a different problem
this code
[code]
$query = "SELECT user_sec_level FROM security_users WHERE user_name = '$name' AND user_password = '$password'";
$sec_level = @mysql_query($query);
$_SESSION[seclevel] = $sec_level;
[/code]

the problem that $sec_level is a vale of 'resource id #5' i need it to be the value that is in that that location in the table....

im missing a step... any ideas?
Link to comment
Share on other sites

[!--quoteo(post=363894:date=Apr 12 2006, 03:14 PM:name=hadoob024)--][div class=\'quotetop\']QUOTE(hadoob024 @ Apr 12 2006, 03:14 PM) [snapback]363894[/snapback][/div][div class=\'quotemain\'][!--quotec--]
hmm... maybe your problem is that $_SESSION variables are accessed using the following syntax:
$_SESSION['seclevel']
and in your query string, you're accessing the value using:
$_SESSION[seclevel]
maybe you're just missing the single quotes?
[/quote]
no his syntax is 100% correct. When calling superglobals in something like an echo statement you can have:
[code]
echo 'This session superglobal equals $_SESSION[value]';
[/code]
Link to comment
Share on other sites

[!--quoteo(post=363895:date=Apr 12 2006, 12:19 AM:name=sdaniels)--][div class=\'quotetop\']QUOTE(sdaniels @ Apr 12 2006, 12:19 AM) [snapback]363895[/snapback][/div][div class=\'quotemain\'][!--quotec--]
thanks for the help, it turens out that i have a different problem
this code
[code]
$query = "SELECT user_sec_level FROM security_users WHERE user_name = '$name' AND user_password = '$password'";
$sec_level = @mysql_query($query);
$_SESSION[seclevel] = $sec_level;
[/code]

the problem that $sec_level is a vale of 'resource id #5' i need it to be the value that is in that that location in the table....

im missing a step... any ideas?
[/quote]


TRY THIS Some changes have been made to the way you handle the variable

$query = "SELECT user_sec_level FROM security_users WHERE user_name = '$name' AND user_password = '$password'";
$sec_level = @mysql_query($query);

$set = mysql_fetch_array($sec_level);
$security_level = $set["user_sec_level"];

$_SESSION[seclevel] = $security_level;
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.