OldGrim Posted April 19, 2021 Share Posted April 19, 2021 (edited) CMS: PHP-Fusion version 9.03.110, PHP version: 7.4.16, MySQL Server Version: 5.7.33-log, DB Driver: PDO MySQL My own support people at PHP-Fusion main site have been unable to help me with this problem and I'm hoping for some new eyes and new input on this error. First of all my script works perfectly fine and outputs the result desired. I know that my script is not using PDO prepared statements and I am not prepared to modify it to do so because too much would have to be changed. And I'm not fluent on PDO. My script is an event calendar and can be seen at this url: https://windy.whisperwillow.net/grims_blog/index.php The error only occurs 3 times on 3 lines. <?php echo "<div class='col-sm-12'>\n"; include BASEDIR."grims_blog/include/cal_func_inc.php"; $day=date('d'); $endDate=date('t',mktime(0,0,0,$cMonth,$day,$cYear)); echo "<div><span class='hdspan2'><b>".$locale['gb_510']."</b></span></div><p>\n"; echo "<table class='tbl-responsive' width='67%' align='center' border='0' cellpadding='0' cellspacing='0'><tr>\n"; echo "<td align='center' colspan='3'><img style='max-width:100%;height:auto;' src='".BASEDIR."grims_blog/images/calhead/forest.jpg'></td>\n"; echo "</tr><tr height='30' bgcolor='#960000'>\n"; echo "<td align='left'> <a class='mcaldate' href='".FUSION_SELF."?month=$prev_month&year=$prev_year' title='Last Month'>".$locale['gb_511']."</a></td><td align='center'><span class='mnthhd'>".date("F Y",strtotime($cYear."-".$cMonth."-01"))."</span></td><td align='right'><a class='mcaldate' href='".FUSION_SELF."?month=$next_month&year=$next_year' title='Next Month'>".$locale['gb_512']."</a> </td>\n"; echo "</tr></table>\n"; echo "<table width='67%' align='center' border='0' cellpadding='0' cellspacing='0'><tr><td class='cal-blank'>\n"; echo "<table width='100%' align='center' border='0' cellpadding='0' cellspacing='0'><tr bgcolor='#005E00' height='35'>\n"; foreach ($short_days as $key=>$val) { echo "<th style='text-align:center' width='14%'><span style='font-size:12px; color:yellow;'><b>".$val."</b></span></th>\n"; } echo "</tr><tr>\n"; $s=date('w', mktime (0,0,0,$cMonth,1,$cYear)); for ($ds=1; $ds<=$s; $ds++) { echo "<td class='cal-blank' height='35' style='text-align:center;' valign='middle'></td>\n"; } for ($d=1; $d<=$endDate; $d++) { if (date('w',mktime (0,0,0,$cMonth,$d,$cYear)) == 0) { echo "<tr>\n"; } $result = dbquery("SELECT post_id, post_title, post_date FROM ".DB_GRIMS_BLOG_POST." WHERE active='1' AND MONTH(post_date) = '$cMonth' AND DAY(post_date) = '$d'"); $data = dbarray($result); $stuff1 = $data['post_id'] ? $data['post_id'] : ""; if ($stuff1) { $id = $stuff1; } else { $id = ""; } $stuff2 = $data['post_title'] ? $data['post_title'] : ""; if ($stuff2) { $title = $stuff2; } else { $title = ""; } $stuff3 = strtotime($data['post_date']) ? strtotime($data['post_date']) : ""; if ($stuff3) { $evday = $stuff3; } else { $evday = ""; } if (date("d", intval($evday)) == $d) { echo "<td height='35' class='event' align='center' valign='middle'>\n"; } elseif ($d == $day && $cMonth == date('m') && $cYear == date('Y')) { echo "<td height='35' class='today' align='center' valign='middle'>\n"; } else { echo "<td height='35' class='cal-norm' align='center' valign='middle'>\n"; } if ($d == $day && $cMonth == date('m') && $d <> $evday) { echo "<a class='cal2' href='".BASEDIR."grims_blog/filtered.php?post_id=$id' title='".$locale['gb_513']." $title'><b>$d</b></a>\n"; } elseif ($d == $day && $cMonth == date('m') && $d == $evday) { echo "<a class='cal2' href='#'><b>$d</b></a>\n"; } elseif ($evday) { echo "<a class='ecal' href='".BASEDIR."grims_blog/filtered.php?post_id=$id' title='$title'><b>$d</b></a>\n"; } else { echo "<span class='noevt'><b>$d</b></span>\n"; } echo "</td>\n"; if (date('w',mktime (0,0,0,$cMonth,$d,$cYear)) == 6) { echo "</tr>\n"; } } echo "</table></td></tr></table>\n"; echo "</td></tr></table><p></div>\n"; ?> Well I could not add comments to the posted script. These 3 lines of the db query area give the error in my error log: 1) $stuff1 = $data['post_id'] ? $data['post_id'] : ""; /* Trying to access array offset on value of type bool */ 2) $stuff2 = $data['post_title'] ? $data['post_title'] : ""; /* Trying to access array offset on value of type bool */ 3) $stuff3 = strtotime($data['post_date']) ? strtotime($data['post_date']) : ""; /* Trying to access array offset on value of type bool */ I know I am asking a lot for help on older code but would really appreciate it if someone can take a look. Edited April 19, 2021 by OldGrim Clarify error lines Quote Link to comment https://forums.phpfreaks.com/topic/312508-how-to-solve-trying-to-access-array-offset-on-value-of-type-bool/ Share on other sites More sharing options...
Barand Posted April 19, 2021 Share Posted April 19, 2021 Does var_dump($data) shed any light? Quote Link to comment https://forums.phpfreaks.com/topic/312508-how-to-solve-trying-to-access-array-offset-on-value-of-type-bool/#findComment-1585959 Share on other sites More sharing options...
OldGrim Posted April 19, 2021 Author Share Posted April 19, 2021 This is the output of var_dump($data); array(3) { ["post_id"]=> string(2) "35" ["post_title"]=> string(8) "Fake Day" ["post_date"]=> string(10) "2021-04-01" } bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) array(3) { ["post_id"]=> string(2) "36" ["post_title"]=> string(12) "Carbon Tales" ["post_date"]=> string(10) "2021-04-18" } bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) Quote Link to comment https://forums.phpfreaks.com/topic/312508-how-to-solve-trying-to-access-array-offset-on-value-of-type-bool/#findComment-1585962 Share on other sites More sharing options...
Barand Posted April 19, 2021 Share Posted April 19, 2021 Your dbarray() function seems to return some weird stuff. Quote Link to comment https://forums.phpfreaks.com/topic/312508-how-to-solve-trying-to-access-array-offset-on-value-of-type-bool/#findComment-1585964 Share on other sites More sharing options...
OldGrim Posted April 19, 2021 Author Share Posted April 19, 2021 Actually it's dead on. If you count the 2 events plus the bool{false} items they total 30 items (30 days in April). The dbquery is inside the forLoop which counts and prints the days of the month in the calendar. I have tried many many different scenarios of having the query outside the forLoop but they just won't work. If I could solve the "bool{false}" counts I'd be home free. In older versions of PHP this wasn't a problem but if I lower the active PHP version version it causes other problems that are even more difficult to solve. I may be asking the impossible here but I thought I'd try. You guys are all top notch PHP people. Quote Link to comment https://forums.phpfreaks.com/topic/312508-how-to-solve-trying-to-access-array-offset-on-value-of-type-bool/#findComment-1585965 Share on other sites More sharing options...
Barand Posted April 19, 2021 Share Posted April 19, 2021 So you called dbarray() 30 times. Twice it returned the array containing post data and 28 times it returned false. Yet every time you try to process it as though they all contain post data. Check that $data is not false before trying to get the $stuff. Quote Link to comment https://forums.phpfreaks.com/topic/312508-how-to-solve-trying-to-access-array-offset-on-value-of-type-bool/#findComment-1585967 Share on other sites More sharing options...
Solution OldGrim Posted April 19, 2021 Author Solution Share Posted April 19, 2021 Thanks again Barand. This now works fine. $result = dbquery("SELECT post_id, post_title, post_date FROM ".DB_GRIMS_BLOG_POST." WHERE active='1' AND MONTH(post_date) = '$cMonth' AND DAY(post_date) = '$d'"); $data = dbarray($result); if (empty($data['post_id'])) { $id = null; } else { $id = $data['post_id']; } if (empty($data['post_title'])) { $title = null; } else { $title = $data['post_title']; } if (empty($data['post_date'])) { $evday = null; } else { $evday = strtotime($data['post_date']); } As I said before you guys are the greatest! Mainly because you know how to make US work for it and figure it out ourselves. Quote Link to comment https://forums.phpfreaks.com/topic/312508-how-to-solve-trying-to-access-array-offset-on-value-of-type-bool/#findComment-1585968 Share on other sites More sharing options...
Barand Posted April 19, 2021 Share Posted April 19, 2021 or (cleaner) ... $data = dbarray($result); if (!$data) { $id = $title = $evday = null; } else { $id = $data['post_id']; $title = $data['post_title']; $evday = strtotime($data['post_date']); } Â Quote Link to comment https://forums.phpfreaks.com/topic/312508-how-to-solve-trying-to-access-array-offset-on-value-of-type-bool/#findComment-1585969 Share on other sites More sharing options...
OldGrim Posted April 19, 2021 Author Share Posted April 19, 2021 3 hours ago, Barand said: $data = dbarray($result); if (!$data) { $id = $title = $evday = null; } else { $id = $data['post_id']; $title = $data['post_title']; $evday = strtotime($data['post_date']); } Do you actually mean: $data = dbarray($result); if (!$data) { $id = null; $title = null; $evday = null; } else { $id = $data['post_id']; $title = $data['post_title']; $evday = strtotime($data['post_date']); } Â Quote Link to comment https://forums.phpfreaks.com/topic/312508-how-to-solve-trying-to-access-array-offset-on-value-of-type-bool/#findComment-1585975 Share on other sites More sharing options...
OldGrim Posted April 19, 2021 Author Share Posted April 19, 2021 OK I tried it your way and it still works fine. Slightly less code; thanks man!! Quote Link to comment https://forums.phpfreaks.com/topic/312508-how-to-solve-trying-to-access-array-offset-on-value-of-type-bool/#findComment-1585976 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.