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.

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

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.