Sir I am using these codes on login.php

session_start();
$query="select * from w_log where email ='".$muser."' and pass='".$mypass."'";
$result=sqlsrv_query($con,$query)or die ("Error". sqlsrv_errors($con)) ;
while($res = sqlsrv_fetch_array($result)) {
$_SESSION['id']=($res['id']);
$_SESSION['sno']=($res['usno']);
$_SESSION['user']=($res['name']);
$_SESSION['status']=($res['status']);
}
//header("location:../index.php");
//exit();
echo $_SESSION['status'];

$_SESSION['status'] is equal to GENERAL

But when I call this variable at the top of index.php
session_start();

echo $_SESSION['status'];

It says: ADMIN
Why session $_SESSION['status'] changes its value automatically?

Please help

If the query does not return a result set, then the loop will not assign any value to $_SESSION and the print statement will show the previous value associated with that index: $_SESSION['status'].

I take the above for an example, but if $muser and $mypass are not set then the query will return all rows in the table.

So, if multiple rows are returned by the query, then $_SESSION will be overwritten and show the values of the last loop.

Right after session_start() add:

print print_r($_SESSION, TRUE);

This should tell you if you are carrying the value set by a previous step. Do the same after the loop. And see what you get.

You can also check how the query gets constructed, using similar technique as described above by cereal. Just add this between line 2 and 3:

die($query);

This will print out the query on the screen and stop the script. Now you can inspect the query whether it has all the correct values and no syntax errors. Also you can copy the query and test it in your database directly (e.g. in PhpMyAdmin if you are using it) and see what is returned.

why your coding on line 4 using while?

while($res = sqlsrv_fetch_array($result)) {
  1. if you want to see the list of status, just echo in the while loop. I think it s been replaced with the last value
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.