Paul-D Posted August 5 Share Posted August 5 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 August 5 Share Posted August 5 $_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 August 5 Author Share Posted August 5 (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 August 5 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 August 5 Share Posted August 5 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 August 6 Share Posted August 6 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. 1 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...
Paul-D Posted 14 hours ago Author Share Posted 14 hours ago (edited) Here is the whole statment.php. As you can see on line 14. I have set the value to Show. It can only be changed if the if statment below changes it [Lines 15 and 16]. So this value can not be NULL EVER. I added lines 125 to 128 as a way of capturing null but this never gets trapped EVER? Line 133 failes with the returning null error. I am supplying the functions file SecureFunctionsBankPDO as well. The function GetAllData is on line 28 Statment.php <?php // Banking Version 2.0.0 01-12-2023 Desmond O'Toole. // Banking Version 2.0.0 01-12-2023 Desmond O'Toole. error_reporting(E_ALL); ini_set('display_errors', '1'); require ("secure/SecureFunctionsBankPDO.php"); require ("../secure/SecurePDO.php"); //session_unset(); $page = "Bank Login"; session_start(); Session_Init(); $View = 'Show'; if (isset($_SESSION['CounterValue'])) $View = $_SESSION['CounterValue']; if(!isset($_SESSION["Pk"])) { header('Location: index.php'); exit; } $Pk = $_SESSION["Pk"]; if(KeyCheckX($Pk)== 0) { header('Location: index.php'); exit; } //$EndTime = KeyTestX($Pk, 0); $EndTime = KeyShowX($Pk); $_SESSION["current_page"] = $page; $qBalance = GetBalance(); $Date = $qBalance['EntryDate']; $Value = $qBalance['BalanceValue']; $stamp = strtotime($Date); $StartDate = date('D d-M-Y' ,$stamp); $stamp = $stamp + (WEEKS * 6) + (DAYS * 1); $EndDataDate = date('D d-M-Y' ,$stamp); //$QueryDate = date('d-M-Y' ,$stamp); $curValue = $Value; /* $Cash = number_format(MonthlyTakings(CASH) , 2, ".", ","); $Shops = number_format(MonthlyTakings(SHOPS) , 2, ".", ","); $Online = number_format(MonthlyTakings(ONLINE) , 2, ".", ","); $Other = number_format(MonthlyTakings(OTHER) , 2, ".", ","); $Ebay = number_format(MonthlyTakings(EBAY) , 2, ".", ","); //$Totals = $Cash + $Shops + $Online + $Other; $Totals = number_format($Cash + $Shops + $Online + $Other + $Ebay , 2, ".", ","); */ ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Bank Home 2</title> <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <META http-equiv="imagetoolbar" CONTENT="no"> <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"> <meta http-equiv="Pragma" content="no-cache"> <meta http-equiv="Expires" content="0"> <meta http-equiv="refresh" content="600"> <link rel="stylesheet" href="stylesheets/Library.css" type="text/css"> <link rel="stylesheet" href="stylesheets/LibraryMenu.css" type="text/css"> </head> <body> <div id="PageTitle"> <img src="images/LibraryBanner.jpg" alt=""> </div> <?php include('SideMenu.php'); ?> <div id="PageContent"> <form action="menu.php" method="post"> <span style="color:blue;"><b>Est: 18-July-2015</b></span> <h4><?= "Displayed data up to " . $EndDataDate?><?="<br>Log on ends on " . $EndTime?> <a href="DeleteOldData.php"><span style="color:blue">Clear old data</span></a> <br> </h4> <div style="width:820px;padding:0px;float:left;"> <div style="width:120px;height:10px;padding:10px;float:left;"><b> Date</b> </div> <div style="width:70px;height:10px;padding:10px;float:left;"><b>Money In</b> </div> <div style="width:70px;height:10px;padding:10px;float:left;"><b>Money Out</b> </div> <div style="width:70px;height:10px;padding:10px;float:left;"><b>Balance</b> </div> <div style="width:100px;height:10px;padding:10px;float:left;"><b>Reason</b> </div> <div style="width:230px;height:10px;padding:10px;float:left;"><b>Comment</b> </div> <div style="width:120px;height:4px;padding:8px;text-align:right;float:left;clear:both;"><a style="color:#ae22e2;" href="DataInput.php">* <?=$StartDate?></a> </div> <div style="width:70px;height:4px;padding:10px;text-align:right;float:left;"> </div> <div style="width:70px;height:4px;padding:10px;text-align:right;float:left;"> </div> <div style="width:70px;height:4px;padding:10px;text-align:right;float:left;">£<?=number_format( $Value,2,'.',',')?> </div> <div style="width:100px;height:4px;padding:10px;float:left;"> </div> <div style="width:230px;height:4px;padding:10px;float:left;">Brought forward </div> <?php if($Date === null) { echo "$Date = NULL..."; exit; } if($View === null) { echo "$View = NULL..."; exit; } $stmt = GetAllData($Date,$View); while($row = $stmt->fetch()) { $stamp = strtotime($row['EntryDate']); $ViewDate = date('D d-M-Y' ,$stamp); $CurDate = date('D d-M-Y' ,time()); $input = '£' . $row['Input']; if($row['Input'] == '0.00') $input = ""; $out = '£' . $row['Output']; if($row['Output'] == '0.00') $out = ""; $reason = GetReasonName($row['Reason']); $colorSelect = ColourPicker($row['Reason'],$row['EntryDate']); $AddDate = $row['AddDate']; $x = strtotime($AddDate); $MyDate = date("D dS M H:i", $x); $curValue = $Value + $row['Input'] - $row['Output']; //$curValue = RestoreDecimal($curValue); $DisplayCurValue = number_format( $curValue,2,'.',','); $Value = $curValue; $Comment = $row['Comment']; $Comment = str_replace("£","£", $Comment); // $Comment, "£", "£" $Comment = trim($Comment); $Bold = $row['B']; if($Bold == 1) $Comment = "<b>" . $Comment . "</b>"; $ID = TransposeKey($row['Tag']); //$ID = $row['Tag']; $x = ""; if($colorSelect == "#777777") { ?> <div style="width:120px;height:4px;padding:10px;text-align:right;float:left;text-color:green;clear:both;"><dfn title="Test"></dfn><?=$ViewDate?> </div> <?php } else { ?> <div style="width:115px;height:4px;padding:10px;text-align:right;float:left;text-color:green;clear:both;"> <a href="StatementEntry.php?ID=<?=$ID?>" title="<?=$MyDate?>"> <?=$ViewDate?></a></div> <?php } ?> <div style="width:70px;height:4px;padding:10px;color:<?=$colorSelect?>;<?=$x?>text-align:right;float:left;"><?=$input?> </div> <div style="width:70px;height:4px;padding:10px;color:<?=$colorSelect?>;<?=$x?>text-align:right;float:left;"><?=$out?> </div> <div style="width:70px;height:4px;padding:10px;color:<?=$colorSelect?>;<?=$x?>text-align:right;float:left;">£<?=$DisplayCurValue?> </div> <div style="width:100px;height:4px;padding:10px;color:<?=$colorSelect?>;<?=$x?>float:left;"><?=$reason?> </div> <div style="width:250px;height:4px;padding:10px;color:<?=$colorSelect?>;<?=$x?>float:left;"><?=$Comment?> </div> <?php } ?> <div style="width:70px;height:1px;padding:10px;float:left;clear:both"> </div> </div> <div style="width:670px;height:50px;padding:0px;float:left;"> <input class="MyButton"type="submit" name="direction" value="Main Menu"> <br> </div> </form> </div> </body> </html> SecureFunctionsBankPDO <?php //include_once ("../secure/SecurePDO.php"); error_reporting(E_ALL); ini_set('display_errors', '1'); define ('OTHER', 1); define ('SHOPS', 10); define ('ONLINE', 11); define ('CASH', 12); define ('EBAY', 15); define ('ARGOS', 16); // System time values define ('MINUTES',60); define ('HOURS',(MINUTES * 60)); define ('DAYS',(HOURS * 24) ); define ('WEEKS',(DAYS * 7)); global $View; function GetBalance() { $pdo = connectDB(); $stmt = $pdo->query("SELECT * FROM Bank_config"); $row = $stmt->fetch(); return $row; } function GetAllData($StartDate, $View) { StoreData($StartDate ."-" . $View); // *** Store data to trap this error *** $pdo = connectDB(); if($View == 'Show' or $View == 'Show+' ) //Desmond. or Desmond.. { $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; } } function MonthlyTakings($Cat) // E2011 { $pdo = connectDB(); //$sqlWithdraw = "SELECT Sum(Output) FROM Bank_Data WHERE EntryDate >= '2015-07-07' AND EntryDate <= '2015-08-07' AND (Reason = 1 OR Reason = 10) ORDER BY EntryDate"; $sqlWithdraw = "SELECT Sum(Output) FROM Bank_Data WHERE EntryDate > DATE_ADD( CURDATE() ,INTERVAL -30 DAY) AND EntryDate <= DATE_ADD( CURDATE() ,INTERVAL 0 DAY) AND (Reason = $Cat) ORDER BY EntryDate"; $stmt = $pdo->query($sqlWithdraw); //echo $sqlWithdraw; //exit; $row = $stmt->fetch(); if($row["Output"] != "") $ret = $row[0]; else $ret = "0.00"; return $ret; } function GetReasonName($r) // E2006 { $pdo = connectDB(); $sqlReason = "SELECT Reason FROM Bank_Reason WHERE ReasonID = " . $r; $stmt = $pdo->query($sqlReason); $row = $stmt->fetch(); return $row["Reason"]; } function ColourPicker($ref,$date) // E2007 { $CurDate = time(); $EntryDate = strtotime($date); $Diff = $EntryDate - $CurDate + (DAYS * 3); // Entries allowed to be edited. Less than 0 = Disabled. if($_SESSION['CounterValue'] == "Show") //Desmond. $Diff = $EntryDate - $CurDate + (DAYS * 14); if($_SESSION['CounterValue'] == "Total") //Desmond... $Diff = $EntryDate - $CurDate + (DAYS * 7); if($_SESSION['CounterValue'] == "Database") //Desmond+ $Diff = 1; if ($Diff <0) $Diff = -1; else $Diff = 1; $val = "#000000"; if($ref == 1) $val = "#0000ff"; // Other Blue if($ref == 4) $val = "#ff00ff"; // CT Pink if($ref == 5) $val = "#c51010"; // Energy Burgandy if($ref == 6) $val = "#27b30b"; // TT_M Dark green if($ref == 7) $val = "#06b8b6"; // TT_BB Cyan if($ref == 8) $val = "#00aa00"; // MNOPF Green if($ref == 9) $val = "#aa7700"; // Water Brown if($ref == 10) $val = "#ff0000"; // Shop Red if($ref == 11) $val = "#7777ff"; // Online light Blue if($ref == 15) $val = "#7a061c"; // Ebay Royal green if($ref == 18) $val = "#aa00aa"; // HSBC if($ref == 19) $val = "#aa7700"; // Amazon Orders Brown if($ref == 20) $val = "#301de8"; // State Pension if ($Diff == -1) $val = "#777777"; return $val; } function DeleteOldData() { $pdo = connectDB(); $Date = time(); $Month = date('m' ,$Date); $Year = date('Y' ,$Date); $Month -= 1; if ($Month == 0) { $Month = 12; $Year -= 1; } $Date = $Year . "-" . $Month . "-01"; $sqlOldData = "DELETE from `Bank_Data` Where EntryDate < ? "; // $sqlOldData = "DELETE FROM Bank_Data WHERE EntryDate < CURDATE() - INTERVAL 1 MONTH"; // date('Y-m-d', strtotime('first day of last month')) $stmt = $pdo->prepare($sqlOldData); $return = $stmt->execute([$Date]); } function greeting() // E102 { $pdo = connectDB(); $sql = "SELECT UserF, UserS FROM LIBusersX WHERE User = '" . $_SESSION['Uk'] . "'"; $stmt = $pdo->query($sql); $rs = $stmt->fetch(); echo $rs['UserF'] . " " . $rs['UserS']; } function GetReason() // E2001 { $pdo = connectDB(); $sqlList = "SELECT * FROM Bank_Reason Order by Reason ASC"; $stmt = $pdo->query($sqlList); //$qList = mysql_query($sqlList) or die ("E2001-01A"); return $stmt; } function AddRecord($EntryDate,$input,$out,$Reason,$Comment) // E2002 { $pdo = connectDB(); $AddDateStamp = time(); $AddDate = date("Y-m-d H:i", $AddDateStamp); $Transaction = dechex(strtotime($EntryDate)); $Insertion = dechex(time()); $package = md5($Transaction . $Insertion); $Hilight = 0; $sqlGetCount = "Select * FROM Bank_Data"; $stmt = $pdo->prepare($sqlGetCount); $stmt->execute(); $Count = $stmt->rowCount(); $Count = dechex($Count += 256); $Tag = substr(($Count . $Transaction . $Insertion . $package),0,32); $sqlList = " INSERT INTO Bank_Data SET EntryDate = :Entry, Input = :In, Output = :Out, Reason = :Reason, Comment = :Comment, AddDate = :AddDate, Tag = :Tag, B = :B "; $stmt = $pdo->prepare($sqlList); $stmt->execute([ ':Entry' => $EntryDate, ':In' => $input, ':Out' => $out, ':Reason' => $Reason, ':Comment' => $Comment, ':AddDate' => $AddDate, ':Tag' => $Tag, ':B' => $Hilight ]); return; } function UpdateBalance($BF,$EntryDate) { $pdo = connectDB(); //echo $BF . " - " . $EntryDate; //exit; $sqlBalanceUpdate = "UPDATE Bank_config SET EntryDate = ?, BalanceValue = ?"; $stmt = $pdo->prepare($sqlBalanceUpdate); $stmt->execute([$EntryDate, $BF]); return; } function EntryDetails($Tag) // E2008 { $pdo = connectDB(); $sqlDataEntry = "SELECT * FROM Bank_Data WHERE Tag = ?"; $stmt = $pdo->prepare($sqlDataEntry); $stmt->execute([$Tag]); return $stmt; } function UpdateRecord($EntryDate,$input,$out,$Reason,$Comment,$Bold) // E2009 { $pdo = connectDB(); $Comment = addslashes($Comment); $Tag = $_SESSION['Tag']; $sqlList = "UPDATE Bank_Data SET EntryDate = :Entry, Input = :In, Output = :Out, Reason = :Reason, Comment = :Comment, B = :Bold WHERE Tag = :Tag "; $stmt = $pdo->prepare($sqlList); $stmt->execute([ ':Entry' => $EntryDate, ':In' => $input, ':Out' => $out, ':Reason' => $Reason, ':Comment' => $Comment, ':Bold' => $Bold, ':Tag' => $Tag ]); return; } function DeleteRec($Tag) // E2010 { $pdo = connectDB(); $sqlDeleteRec = "DELETE FROM Bank_Data WHERE Tag = '" . $Tag . "'"; $stmt = $pdo->prepare($sqlDeleteRec); $stmt->execute(); //$stmt->execute([$Tag]); } ?> One point here is that in the function GetAllData, I have added a trap that logs the values passed to this function in a database table Line 36 shows that a variable is missing. I should have the date follows by '-' and the variable $View so I should see '2025-07-31-Show' but on line 36 I get '2025-07-31-'. The variable $View is clearly missing. (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'), These are all the files needed here.. Edited 14 hours ago by Paul-D Quote Link to comment https://forums.phpfreaks.com/topic/330113-ongoing-problem-with-missing-session-variables/#findComment-1659407 Share on other sites More sharing options...
mac_gyver Posted 9 hours ago Share Posted 9 hours ago you still haven't done this - On 8/5/2025 at 6:34 AM, mac_gyver said: 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. so, you don't know what the non-printing value actually is. it is likely an empty string '', not a null value. the null in the "Call to a member function fetch() on null" error is because GetAllData() returns nothing (null) when $View doesn't match one of he 4 values. you haven't posted ../secure/SecurePDO.php or SideMenu.php. where is session_init() defined? it's likely setting $_SESSION['CounterValue'] to the offending value. since this code could be redirecting to index.php and back to statement.php, whet is the full code being used on index.php (which i already asked you to post)? do any of these filenames being required/included exist at different paths within your project, so that different parts of your code could be requiring/including a wrong version of a file? i ask this because in the related code you have posted for this problem, there have been different versions of session_init() and variables being used that haven't been assigned a value in any of the posted code. 1 Quote Link to comment https://forums.phpfreaks.com/topic/330113-ongoing-problem-with-missing-session-variables/#findComment-1659409 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.