Sir I am using these codes on login.php

$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)) {
echo $_SESSION['status'];

$_SESSION['status'] is equal to GENERAL

But when I call this variable at the top of index.php

echo $_SESSION['status'];

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

Please help

5 Months
Discussion Span
Last Post by ahmad.albab.87

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:


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
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.