Link to home
Start Free TrialLog in
Avatar of Kevin Smith
Kevin SmithFlag for United States of America

asked on

How do I create a select join query in MySQL with a session?

How do I do a table join in PHP using MySQL and query the data in sessions?

My registered sessions come from a form (SSN and pass), but the table will be related by a member_ID column.  How do I apply this to my pages?

Thanks!
Kevin
Avatar of phirana
phirana
Flag of United Kingdom of Great Britain and Northern Ireland image

select session.ssn, member_ID from sessions, member

This will join the tables for you.
Avatar of Kevin Smith

ASKER

?

So 'sessions' will recognize the sessons fields?
select table.column from table.

This asks for the column in the table
Okay, lemme rephrase:

I have a page getting data from TABLEB.  The session has been created from a form that gets it's data from TABLEA.  TableA and TableB have a related field (member_ID).  I'm on a page that I want to pull TABLEB data from that has the same session information from the beginning.  Or would it be better just to hide the member_Id in the session?
i  would place information like that in the session its self - you shouldn't hide information like that in forms
okay...so I've put the data in a session, and now in my query I have this:

$result=mysql_query("select * from mbr_body_image  
  where member_ID='member_ID'");

does that seem right?  Or do I need to call it from the session (and not named directly)?
ASKER CERTIFIED SOLUTION
Avatar of phirana
phirana
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cedric Obinna A.
If you store your session in a variable called session_var, then you could use

$result=mysql_query("select * from mbr_body_image  
  where member_ID='session_var'");

This should work fine given that session_var was created from TABLEa nad mbr_body_image is TABLEb.
nad = and in my last post.... sorry
This is my whole query page code...not working...can you see anything I'm doing wrong?

<?
// start session
session_start();  

// convert username and password from _POST or _SESSION
if($_POST){
  $_SESSION['SSN']=$_POST["SSN"];
  $_SESSION['pass']=$_POST["pass"];  
  $_SESSION['member_ID']=['members_ID'];
}

// query for a user/pass match
$result=mysql_query("select * from mbr_body_image  
  where member_ID='" . $_SESSION['member_ID'] . "'");

// retrieve number of rows resulted
$num=mysql_num_rows($result);  
 if(mysql_num_rows($result)==1)
 
 
{
    $resultArray = mysql_fetch_array($result);
      
}
else
{
    echo "Error retrieving data";
}

// print login form and exit if failed.
if($num < 1){
  echo "You are not authenticated.  Please login.<br><br>
   
  <form method=POST action=index.php>
  username: <input type=text name=\"SSN\">
  password: <input type=password name=\"pass\">
  <input type=submit>
  </form>";
   
  exit;
}
?>
and I've already noticed the member/members error in the session line..but that line gives me an error on the return anyway.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have kinda the same problem. This query is not producing the result it should.
If I echo $sql I get the following.
select user_name from userslist where postid =91f33dec5cc03a0bcd47879111ccb596
Which is correct, but when I try to use to get results. I get nothing.
If I run it in the sql above in the commandline.. I get the result I want.

It has to do with $_SESSION.
Help appreciated as always.. thanks!
$sql = "select user_name from userslist where postid =". $_SESSION['login'] ." ";
$results = mysql_query( $sql, $db );
echo $results;

Open in new window