Paul-D Posted yesterday at 12:01 PM Share Posted yesterday at 12:01 PM Hi, I am still having problems with a web page. I have talked about this before and nobody could help. I have done more research into this. The webpage Statement.php displays a bank statement within certain dates and how this information is to be displayed using a session variable $_SESSION['CounterValue']. This page gets auto refreshed. I very occasionally get a null error on the function. Uncaught Error: Call to a member function fetch() on null Statement.php Lines 14-17: $View = 'Show'; if (isset($_SESSION['CounterValue'])) $View = $_SESSION['CounterValue']; Statement.php Line 131: $stmt = GetAllData($Date,$View); Session variables do time out but the code should default to = 'Show'. I have been capturing the data before calling the function GetAllData() Data table entries 35, '2025-08-01', '1754065485', '86.1.133.80', '2025-07-31-Show', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36', 'N'), (36, '2025-08-01', '1754079812', '86.1.133.80', '2025-07-31-', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36', 'N'), (37, '2025-08-01', '1754079819', '86.1.133.80', '2025-07-31-Show', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36', 'N'), Entry 36 has '2025-07-31 -' the variable is missing in the call to the function GetAllData. Here is the function in a seperate functions file. Function in separate file function GetAllData($StartDate, $View) { StoreData($StartDate ."-" . $View); // *** Store data to trap this error *** $pdo = connectDB(); if($View == 'Show' or $View == 'Show+' ) { $sqlAllData = "SELECT * FROM Bank_Data WHERE EntryDate > ? AND EntryDate <= DATE_ADD(?, INTERVAL 6 WEEK) ORDER BY EntryDate ASC, Output"; $stmt = $pdo->prepare($sqlAllData); $stmt->execute( [ $StartDate, $StartDate] ); return $stmt; } if($View == 'Total' or $View == 'Database' ) { $sqlAllData = "SELECT * FROM Bank_Data ORDER BY EntryDate ASC, Output"; $stmt = $pdo->prepare($sqlAllData); $stmt->execute(); return $stmt; } } Can anyone find out what is happening to my ($_SESSION['CounterValue'] and why is it not defaulting to $View = 'Show' This is driving me mad now as it has been going on ever since I upgrades this to PDO last year. Quote Link to comment https://forums.phpfreaks.com/topic/330113-ongoing-problem-with-missing-session-variables/ Share on other sites More sharing options...
mac_gyver Posted 23 hours ago Share Posted 23 hours ago $_SESSION['CounterValue'] may be set, but doesn't contain what you think. either use var_dump() on $view to display what it is or use var_export(), with the 2nd parameter set to true, when you supply it to the StoreData() call to cause it's value (empty string '', null, false, or true) to be used. best guess is you have some code assigning a value to it, using one =, instead of testing the value in it using two == or three ===. Quote Link to comment https://forums.phpfreaks.com/topic/330113-ongoing-problem-with-missing-session-variables/#findComment-1657999 Share on other sites More sharing options...
Paul-D Posted 16 hours ago Author Share Posted 16 hours ago (edited) On log in depending on the user the session can only be set to 1 of the 4 valuse. Show, Show+, Database or Total. So if it is set it will have one of them. By default it is set to Show. Also it is stable for quite some time before it errors on an auto refresh. Either way it will contain a valid string. Edited 16 hours ago by Paul-D Quote Link to comment https://forums.phpfreaks.com/topic/330113-ongoing-problem-with-missing-session-variables/#findComment-1658030 Share on other sites More sharing options...
mac_gyver Posted 15 hours ago Share Posted 15 hours ago obviously it is not. you must determine what the non-printing value actually is and find where in your code it's being set to that value. you either have an assignment, instead of a comparison, like i already wrote, or you have some code that's running, such as after a redirect, where you didn't stop php code execution, and it's assigning a value that when echoed is an empty value. Quote Link to comment https://forums.phpfreaks.com/topic/330113-ongoing-problem-with-missing-session-variables/#findComment-1658031 Share on other sites More sharing options...
mac_gyver Posted 4 hours ago Share Posted 4 hours ago this is apparently the previous related thread - https://forums.phpfreaks.com/topic/321226-pdo-error-on-function-since-a-migration-to-pso at that time, the "Statement.php Lines 14-17" were in a file being included before the session_start() and would never have found the session variable set. i'm going to guess that the "fetch() on null error" was occurring on every page request? you have since moved these lines to statement.php. after reviewing the code you posted previously, if this problem is only occurring occasionally, it is likely due to some auto logout logic, a page auto reload occurring exactly at the point of being logged out, and a timing/race condition in the logic, due to all these session variables. at the risk or repeating myself. the only piece of user related data you should store in a session variable upon successful login is the user id (autoincrement primary index.) you should query on each page request to get any other user data, so that any changes made to this other user data take effect on the very next page request. if you cannot determine the cause of this problem, you will need to post all the current code for statemnt.php, everything being included (you should use require for things your code must have) by statement.php, everything those files are including, and index.php (and everything it includes and everything those files include) since it is involved in the redirects and could be redirecting back to statement.php, less any database credentials or sensitive site information (which should be in a configuration .php file), for anyone here to be able to help. Quote Link to comment https://forums.phpfreaks.com/topic/330113-ongoing-problem-with-missing-session-variables/#findComment-1658037 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.