accces session data

I am trying to create a session so that I can match students, companyID with that of the logged on coordinator, which is also a student but has access to the coordinator page.
So the coordinator companyid is "abc123" in column CompanyID in Table Students.
I want it to load all other students that have that same companyid, so the coordinator can see them.
problem is that I am getting no errors but it is not creating the session...that I can tell? and is not showing any studnets.


if (isset($session_data["CurCompany"]))  
{
 $smarty->assign("cur_company",$session_data["CurCompany"]);

if (!($result = mysql_query("SELECT * FROM STUDENTS WHERE CompanyID=".$session_data["CurCompanyID"], $link)))
{
  DisplayErrMsg(sprintf("internal error %d:%s\n", mysql_errno(), mysql_error()));
  return 0;
 }
 $row = mysql_fetch_array($result);
 $session_data["CurCompanyID"] = $curcompanyid;

$showmy = array();
while ($row = mysql_fetch_array($result)) {
    $showmy[] = array(
       "CompanyID" => $row["CompanyID"],
       "StudentID" => $row["StudentID"],
       "Date" => date('m-d-Y', $row["Date"]),
    );
}
mysql_free_result($result);

$smarty->assign("companyid", $showmy);
}
 
html page code--->

       <td align="left" valign="top">{foreach key=key item=item from=$companyid}<br>
    <font class="text" id="clr_red">Company ID:</font> {$item.CompanyID} <font class="text" id="clr_red">Student ID:</font> {$item.StudentID}<font class="text" id="clr_red"> Date:</font> {$item.Date}
<br>{/foreach}</td>
tarakwar101Asked:
Who is Participating?
 
TomeeboyCommented:
Heh...I see the problem (or so I think).  You changed the name of the first result to $new_result, but we didn't update this line to reflect the change:

$row = mysql_fetch_array($result);

Change it to:

$row = mysql_fetch_array($new_result);
0
 
TomeeboyCommented:
Do you have:

session_start();

at the top of your script?
0
 
tarakwar101Author Commented:
yes
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
tarakwar101Author Commented:
If I add this, will it make a difference?
 if (!($result = mysql_query("SELECT * FROM STUDENTS WHERE (StudentID='".$session_data["CurStudentID"]."' AND CompanyID='".$session_data["CompanyID"]."')", $link)))
0
 
tarakwar101Author Commented:
$_SESSION['CompanyID'] = "CompanyID";
if (isset($companyid))
 {
 $smarty->assign("cur_student",$session_data["CurStudent"]);
 
if (!($result = mysql_query("SELECT * FROM STUDENTS WHERE (StudentID='".$session_data["CurStudentID"]."' AND CompanyID='".$session_data["CompanyID"]."')", $link)))
{
  DisplayErrMsg(sprintf("internal error %d:%s\n", mysql_errno(), mysql_error()));
  return 0;
 }
$row = mysql_fetch_array($result);
 $session_data["CompanyID"] = $companyid;
}
$showmy = array();
while ($row = mysql_fetch_array($result)) {
    $showmy[] = array(
       "CompanyID" => $row["CompanyID"],
       "StudentID" => $row["StudentID"],
       "Date" => date('m-d-Y', $row["Date"]),
    );
}
mysql_free_result($result);

$smarty->assign("companyid", $showmy);
0
 
TomeeboyCommented:
Try changing this section:

if (!($result = mysql_query("SELECT * FROM STUDENTS WHERE CompanyID = ".$session_data["CurCompanyID"], $link)))
{
  DisplayErrMsg(sprintf("internal error %d:%s\n", mysql_errno(), mysql_error()));
  return 0;
 }

To this:

$sql = "SELECT * FROM STUDENTS WHERE CompanyID=" . $session_data['CurCompanyID'];
$result = mysql_query($sql, $link) or die(mysql_error());

Also, is the company ID a string or integer?  You might need to put single quotes around it:

$sql = "SELECT * FROM STUDENTS WHERE CompanyID = '" . $session_data['CurCompanyID'] . "'";
0
 
tarakwar101Author Commented:
It is a string IE: sml123
so single quotes for int and double for string?
I am still learning the basics on alot of this stuff. lol
0
 
TomeeboyCommented:
No, when doing your query in MySQL, you can usually get away with NO quotes around a number, but for strings you need to have single quotes around them.  So use this line:

$sql = "SELECT * FROM STUDENTS WHERE CompanyID = '" . $session_data['CurCompanyID'] . "'";

I know all the quotes there can make it confusing, but what it comes out to is basically this:

SELECT * FROM STUDENTS WHERE CompanyID = 'sml123'
0
 
tarakwar101Author Commented:
I get no errors but still no students records are showing up?
0
 
TomeeboyCommented:
Are you sure a result is getting returned?  Try this:

if (mysql_num_rows($result) == 0) {
     die("Result came back empty!");
}

Right before this line:

$row = mysql_fetch_array($result);
0
 
tarakwar101Author Commented:
I did this, but the issue now is that it shows all the students in DB not just the ones with the companyid of the logged in user.
I know I am close I just need a way to only have it show those users with the same companyid as logged in user... I will go back to other code and try your above code, but first I will wait for a reply from you on the ciode below.


if (isset($session_data["CurStudent"]))  
{
 $smarty->assign("cur_student",$session_data["CurStudent"]);

 if (!($new_result = mysql_query("SELECT * FROM STUDENTS WHERE StudentID=".$session_data["CurStudentID"], $link)))
 {
  DisplayErrMsg(sprintf("internal error %d:%s\n", mysql_errno(), mysql_error()));
  return 0;
 }
 
$row = mysql_fetch_array($result);
$session_data["CompanyID"] = $row["CompanyID"];

if (!($result = mysql_query("SELECT * FROM STUDENTS", $link)))
{
 DisplayErrMsg(sprintf("internal error %d:%s\n", mysql_errno(), mysql_error()));
 return 0;
 }
$row = mysql_fetch_array($result);
 $session_data["CompanyID"] = $row["CompanyID"];;
{
$showmy = array();
while ($row = mysql_fetch_array($result)) {
    $showmy[] = array(
       "CompanyID" => $row["CompanyID"],
       "StudentID" => $row["StudentID"],
       "Date" => date('m-d-Y', $row["Date"]),
    );
}
  }
    }
mysql_free_result($result);

$smarty->assign("companyid", $showmy);
0
 
TomeeboyCommented:
Try this:

if (isset($session_data["CurStudent"]))  
{
 $smarty->assign("cur_student",$session_data["CurStudent"]);

 if (!($new_result = mysql_query("SELECT * FROM STUDENTS WHERE StudentID=".$session_data["CurStudentID"] . " LIMIT 1", $link)))
 {
  DisplayErrMsg(sprintf("internal error %d:%s\n", mysql_errno(), mysql_error()));
  return 0;
 }
 
$row = mysql_fetch_array($result);
$session_data["CompanyID"] = $row["CompanyID"];

if (!($result = mysql_query("SELECT * FROM STUDENTS WHERE CompanyID = " . $session_data['CompanyID'] . ", $link)))
{
 DisplayErrMsg(sprintf("internal error %d:%s\n", mysql_errno(), mysql_error()));
 return 0;
 }

$showmy = array();
while ($row = mysql_fetch_array($result)) {
    $showmy[] = array(
       "CompanyID" => $row["CompanyID"],
       "StudentID" => $row["StudentID"],
       "Date" => date('m-d-Y', $row["Date"]),
    );
}
  }
    }
mysql_free_result($result);

$smarty->assign("companyid", $showmy);
0
 
TomeeboyCommented:
Sorry, change this line:

if (!($result = mysql_query("SELECT * FROM STUDENTS WHERE CompanyID = " . $session_data['CompanyID'] . ", $link)))

To this:

if (!($result = mysql_query("SELECT * FROM STUDENTS WHERE CompanyID = '" . $session_data['CompanyID'] . "'", $link)))
0
 
tarakwar101Author Commented:
Now it post all the students but no companyids here is before and after view on the page.

BEFORE
Company ID: sml123 Student ID: 2 Date: 11-21-2005

Company ID: Student ID: 3 Date: 11-21-2005

Company ID: Student ID: 13 Date: 11-28-2005

Company ID: vargus2323 Student ID: 43 Date: 01-05-2006

Company ID: Student ID: 12 Date: 11-23-2005

AFTER
Company ID: Student ID: 13 Date: 11-28-2005

Company ID: Student ID: 43 Date: 01-05-2006

Company ID: Student ID: 12 Date: 11-23-2005

Company ID: Student ID: 14 Date: 11-28-2005

Company ID: Student ID: 15 Date: 11-28-2005
0
 
tarakwar101Author Commented:
Looking for:
Company ID: sml123 Student ID: 2 Date: 11-21-2005

Company ID: sml123 Student ID: 3 Date: 11-21-2005

Company ID: sml123 Student ID: 13 Date: 11-28-2005

Company ID: sml123 Student ID: 99 Date: 01-05-2006
0
 
TomeeboyCommented:
Try checking any variables that would have a direct impact on your queries, to make sure everything is getting set properly.  For example:

if (isset($session_data["CurStudent"]))  
{
 $smarty->assign("cur_student",$session_data["CurStudent"]);

if(!isset($session_data['CurStudentID']) || empty($session_data['CurStudentID'])) {
     die("Session value: CurStudentID is EMPTY or NOT SET!");
}
 if (!($new_result = mysql_query("SELECT * FROM STUDENTS WHERE StudentID=".$session_data["CurStudentID"] . " LIMIT 1", $link)))
 {
  DisplayErrMsg(sprintf("internal error %d:%s\n", mysql_errno(), mysql_error()));
  return 0;
 }
 
$row = mysql_fetch_array($result);
$session_data["CompanyID"] = $row["CompanyID"];

echo "<p>The company ID returned by new_result was: " . $row['CompanyID'] . "</p>";
if(!isset($session_data['CompanyID']) || empty($session_data['CompanyID'])) {
     die("Session value: CompanyID is EMPTY or NOT SET!");
}

if (!($result = mysql_query("SELECT * FROM STUDENTS WHERE CompanyID = '" . $session_data['CompanyID'] . "'", $link)))

{
 DisplayErrMsg(sprintf("internal error %d:%s\n", mysql_errno(), mysql_error()));
 return 0;
 }

$showmy = array();
while ($row = mysql_fetch_array($result)) {
    $showmy[] = array(
       "CompanyID" => $row["CompanyID"],
       "StudentID" => $row["StudentID"],
       "Date" => date('m-d-Y', $row["Date"]),
    );
}
  }
    }
mysql_free_result($result);

$smarty->assign("companyid", $showmy);
0
 
tarakwar101Author Commented:
The company ID returned by new_result was:

Session value: CompanyID is EMPTY or NOT SET!
0
 
tarakwar101Author Commented:
Excellent!!! You found the fix...Plus you taught me something new, with that:
echo "<p>The company ID returned by new_result was: " . $row['CompanyID'] . "</p>";
if(!isset($session_data['CompanyID']) || empty($session_data['CompanyID'])) {
     die("Session value: CompanyID is EMPTY or NOT SET!");
That will come in handy when debugging.

Thank you very much for your help and patience with me...I am learning more everyday!
Thanks Tomeeboy!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.