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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TomeeboyCommented:
Do you have:

session_start();

at the top of your script?
tarakwar101Author Commented:
yes
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)))
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

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);
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'] . "'";
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
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'
tarakwar101Author Commented:
I get no errors but still no students records are showing up?
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);
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);
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);
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)))
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
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
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);
tarakwar101Author Commented:
The company ID returned by new_result was:

Session value: CompanyID is EMPTY or NOT SET!
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);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.