Jump to content

Can you use session to retrieve mysql data


xxreenaxx1

Recommended Posts

The $_SESSION array shouldn't really be treated any differently to any other array.

 

$sql = "SELECT foo FROM bar WHERE boo = '{$_SESSION['whatever']}'";

 

Of course, if it is user submitted data within the $_SESSION array you will want to make sure to sanitize it.

Link to comment
Share on other sites

my query is sort of messed up. But when I try it on mysql it works. Not sure how to implement that on to PHP. so far I have

 

<Html>
<?php 

MYSQL_CONNECT(localhost,'root','') OR DIE("Unable to connect to database"); 
@mysql_select_db(Examination) or die( "Unable to select database");

?>

<form action="try1.php" method="post">
<?

//$query=("SELECT * FROM subject");
$query=("SELECT DISTINCT * FROM User u, User_X_Subject us, Subject s WHERE u.Use_Name = '{$_SESSION['username']}'
AND u.Use_ID= us.Use_ID 
AND s.Sub_ID=us.Sub_ID");




$result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() );
echo "<select name=myselect>";  

while($row=mysql_fetch_array($result)){ 

echo "<OPTION VALUE=".$row['s.Sub_ID'].">".$row['s.Sub_Name']."</OPTION>";
}

echo "</select>"; 

?>
<input type="submit" value="submit"/>
</form>
</html>

 

but ofcourse its NOT going to work and its NOT working. Help me out

Link to comment
Share on other sites

Firstly, don't use <? tags, ever. Always use <?php

 

Secondly, you have no call to session_start.

 

Thirdly, values being assigned to variables do not need () braces. $foo=('blah'); should simply be $foo = 'blah';

 

die is a poor method of error trapping. Check for errors within an if() statement and handle gracefully.

 

Lastly, what errors are you getting and have you tried echoing your query to see what is actually looks like?

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.