php script works on 7.4 get errors on 8.0

Started by lather, January 08, 2025, 06:29:34 PM

Previous topic - Next topic

lather

I have a php script that works fine on php version 7.4 (which is out of date).
When I change php to 8.0 ai get undefined variable errors from a line like this:

 $reccount = $reccount + 1;

 and undefined array key from this:

$regyear=$row["regyear"];

I have looked at php manuals and tutorials but cannot find what I am doing wrong.




vbgamer45

Need to see more. but means $row["regyear"] is not set/empty
Community Suite for SMF - Take your forum to the next level built for SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com -  Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

Kindred

#2
And you have to set the starting value of the first one
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

live627

What is the desired outcome?

What is the current behavior?

Can you post your code?

lather

Thanks for the replies.
$row["regyear"] is set by a database table query.
code that causes array key error:

$result = $conn->query($sql1);
 if ($result->num_rows > 0) {
   while($row = $result->fetch_assoc()) {
      $regyear=$row["regyear"];
}

that is just a quick sample of the code that is causing errors, the whole script is 390 lines.

To demonstate the errors I have duplicated the script on a subdomain that I set to php 8.1

7.4:
https://www.barrowcreek.com/milesdev/miles.php
8.1:
https://bob.barrowcreek.com/test/miles.php
I appreciate any help.

Illori

if you would like to get further assistance, please post the complete code.

lather

#6
I appreciate the help!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">

<html>
<head>
<title>Mileage Log</title>
<!-- this is the miles asp file I am converting line by line to php  -->
<Script language="javascript">
function doit(roadlog)
                  {
                       if (menuform.roadlog.value == "")
                      // if (menuform.roadlog.value.length < 10)
                        {
                              alert("You can't go there...please make a valid selection");
                              menuform.roadlog.focus();
                              return false;
                        }
                   var url="http://www.wfr-ridersclub.com/Miles/"+menuform.roadlog.value;
                   document.write ("<H3 align='center'><Font color='Blue'>")
                   document.write ("YOU ARE BEING REDIRECTED TO:  " +  url);
document.write ("<BR>IF NOTHING HAPPENS CLICK <a href='" +  url + "'>HERE</a>" );
                   document.write ("</Font></H3>")
                   location.assign(url);
// location.href = url;
return false;
                  }

    function logflag()
                      {
//window.open(URL,name,specs,replace)
win = window.open(", _self", '', 'width=180, height=90, top=100, left=200, toolbar = no, status = no, titelbar = no');
win.document.write("* Indicates rider has posted a road log. Red color indicates post is recent ( less than 24 hours).");
                          }

  function logflagclose()
                      {
                  win.close();
                          }

</script>
<style>
.shadowbox {
  width: 45em;
  border: 1px solid #333;
  box-shadow: 8px 8px 5px #444;
  padding: 8px 12px;
  background-image: linear-gradient(180deg, #fff, #ddd 40%, #ccc);
}
.center {
  margin: auto;
  width: 40%;
 // border: 3px solid #000040;
  padding: 10px;
}

</style>



</head>

<body bgcolor="#D6D3DC">
<h1 align="center"><font color="#0000FF" face="Garamond,Georgia">MILEAGE LOG </font></h1>
<!--<h1 align="center"><font color="red" face="Garamond,Georgia">[OFFLINE FOR MAINTENANCE] </font></h1>-->

<?php
$sort
="";  
$category="ALL";
$clubname="ALL";
$sort"y2025";
// $_POST["selectORsortby"];
// ECHO "<BR>" . $_SERVER["REQUEST_METHOD"] . "<BR>";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
If ($_POST["sort"]<>"") {
$sort=$_POST["sort"];
}Else{
  $sort="y2025";
}  

If ($_POST["category"] <>"") {
  $category=$_POST["category"];
}Else{
$category="ALL";  
}

If ($_POST["clubname"] <>"") {
  $clubname=$_POST["clubname"];
}Else{
$clubname="ALL";
}
}
// ECHO  "<BR> CAT: " . $category . "<BR>";
// ECHO  "<BR> CLUB: " . $clubname . "<BR>";
// ECHO  "<BR> SORT SELECTION: " . $sort . "<BR>";
$servername "localhost";
$username "removed";
$password "removed";
$dbname "barrowcreek_Miles";
// Create connection
$conn = new mysqli($servername$username$password$dbname);

// Check connection
if ($conn->connect_error) {
  die(
"Connection failed: " $conn->connect_error);
}
$sqlall="SELECT ridername from riders WHERE approved ='YES'";
$resultall $conn->query($sqlall);
$total=$resultall->num_rows
//$sqlno="SELECT ridername, approved FROM riders WHERE approved='NO'";
//$resultno = $conn->query($sqlno);
//$nos=$resultno->num_rows;


// sql to select from members table  and join plus eadd up all miles for lifetime sort

$sql1="SELECT  *, lifemiles+y2013+y2014+y2015+y2016+y2017+y2018+y2019+y2020+y2021+y2022+ y2023+y2024+y2025+y2016+y2027+y2028 AS lifesort FROM  riders INNER JOIN RiderMiles USING(ridername)";
// ECHO "QUERY: " . $sql1 ."<BR>";
// sql addition for selecting

If ($category !=="ALL" AND $clubname == "ALL"){
$sql1 $sql1 " WHERE category='";
  $sql1 $sql1 $category "'";
  }
If ($category !=="ALL" AND $clubname !== "ALL"){
$sql1 $sql1 " WHERE category='";
  $sql1 $sql1 $category "'";
$sql1 $sql1 " AND clubname='";
$sql1 $sql1 $clubname "'";
  }

If ($category =="ALL" AND $clubname !=="ALL"){
$sql1 $sql1 " WHERE clubname='";
  $sql1 $sql1 $clubname "'";
  }

 
$sql1 $sql1 " ORDER BY " $sort " DESC";

// ECHO  "<BR>" . $sql1 . "<BR>";
$result $conn->query($sql1);
$selected=$result->num_rows
//$selected=$selected-$nos;
// ECHO "<BR>ROWS: " . $registered;
 
$SQL2 "SELECT `clubname` FROM `riders` WHERE `clubname` <> \" \" ORDER BY clubname";

$result2 $conn->query($SQL2);

?>


<h1 align="center">MOTORCYCLE MILEAGE!</h1>
<h2 align="center">
<?php
date_default_timezone_set
("America/Chicago");

echo  
date("m-d-Y h:i:sa  $d");
ECHO 
$total" RIDERS REGISTERED";
?>

</h2>
<!--
<Table align="center" width="40%">
<TR><TD align="left">
Godaddy has updated our server and the new server does not support the Microsoft Access database that the original Mileage Log used.
I have moved the mileage app to a new server and have converted the database to Mysql and am in the process of rewriting the code in PHP.
The mileage data has been saved as of 4-18-22 which was the last update before the crash. Existing users can update 2022 mileage but conversion
of additional functions, including new user registrations is ongoing.
</td></tr>
</table>
-->
<Div class="center">
<div   class="shadowbox">
<p align="center">If you enjoy using the mileage log invite your riding friends to join.</p>
</div>
</Div>

<h3 align="center"><?php //ECHO "Registered Riders: ".$registered;?></h3>
<table summary="" align="center" border="1" style="border-collapse:collapse" width="80%" cellpadding="3">
<tr>
<td width="25%" align="center"><A href="about.htm">ABOUT</A></td>
<td width="25%" align="center"><a href="registration.php"><b>REGISTER</b></a></td>
<!--<Form name="menuform">-->
<td  width="25%" align="center">
<!--
<select name="roadlog" class="input" onchange="doit()"/>
<Option value="">ROAD LOGS</Option>

<?php
/*

  If rsLogs.Fields("rider"] <> lastrider then
     Response.Write"<Option value=blogentry.php?". rsLogs.Fields("rider"] . ">" 
   Response.write rsLogs.Fields("rider"] . "</Option>"
End if  
' If rsLogs.Fields("logdate"] > lastdate then
' lastdate=rsLogs.Fields("logdate")
lastrider=rsLogs.Fields("rider"]  
End if
 rsLogs.MoveNext
 Loop
 */
 ?>


</select>

</Form>-->



</td>
<td  width="25%" align="center"><A href="contact.htm">CONTACT</A></td>
</tr>
</table>


<table border="1" align="center" cellpadding="2" style="border-collapse:collapse">
<form  name="displayForm" method="Post" action="miles.php">
     <TR>
     <TD><font size="-2"  color="#646464">DISPLAY OPTIONS</font></TD>
<td colspan="2"><select name="category" class="input"  />
<Option value="">SELECT RIDING CATEGORY</Option>
<Option value="ADVENTURE">ADVENTURE (800cc and up)</Option>
<Option value="CRUISER">CRUISER</Option>
<Option value="DUALSPORT">DUAL SPORT (less than 800cc)</Option>
<Option value="SCOOTER">SCOOTER</Option>
<Option value="SPORT">SPORT</Option>
<Option value="SPORT TOURER">SPORT TOURER</Option>
<Option value="STANDARD">STANDARD</Option>
<Option value="TOURER">TOURER</Option>
</select></td>
     <td><select name="clubname" class="input"  />
<Option value="">SELECT A GROUP</Option>
<Option value="">ALL GROUPS</Option>
<?php

// if ($result2->num_rows > 0) {
while($row $result2->fetch_assoc()) {

  If ($row["clubname"] <> $lastclub){
ECHO "<Option>" $row["clubname"] . "</Option>";
$lastclub=$row["clubname"];
}  
 
}
// }

 ?>


</select></td>
 
      <td colspan="3"></td>
      <td><font size="-2" color="#646464">DISPLAYING:</font></td>
      <TD colspan="2"><?php ECHO $selected;?>&nbsp;&nbsp;&nbsp;&nbsp; <?php ECHO $category;?><b> /</b><?php ECHO $clubname;?></td>
     <!--<td></td>-->
     </tr>
   
<form  name="sortForm" method="Post" action="miles.php">
     <TD colspan="3"><font size="-2" color="#646464">SORT ON LIFE MILES</font>
<input type="radio" name="sort" value="lifesort"></td>
<!--<input type="radio" name="sort" value="lifesort"></td> -->
     <td colspan="2"><font size="-2" color="#646464">2024 MILES</font>
    <input type="radio" name="sort" value="y2024" ></td>
      <td colspan="2"><font size="-2" color="#646464">2025 MILES</font>
      <input type="radio" name="sort" value="y2025" checked></td>
      <TD colspan="3"><input type="submit" value="SORT/SELECT"></td>
     <!--<td></td>-->
     </tr>
     </form>
<TR><td colspan="6" align="center"><font size="-1" color="#0062C4">Click your rider name link to update mileage or your profile.</font></td></tr>
     <TR bgcolor="black">
     <TD nowrap="nowrap"><font color="#F27555"><Div OnMouseOver="logflag()" OnMouseOut="logflagclose()">RIDER *</Div></font></TD>
<TD><font color="#F27555">AGE</font></TD>
<td><font color="#F27555">MAIN RIDE</font></td>
<td><font color="#F27555">OTHER RIDE</font></td>
<td><font color="#F27555">CLUB</font></td>
     <td><font color="#F27555">LIFETIME</font></td>
     <td><font color="#F27555">2024</font></td>
     <td><font color="#F27555">2025</font></td>

      <td><font color="#F27555">UPDATED</font></td>

      <td><font color="#F27555">PER DAY</font></td>
      <td><font color="#F27555">PROJECTED </font></td>
      <!--<TD><font color="#F27555">PACE</font></td>-->

     </tr>


<?php
$dayofyear
date('z')+1;
/*
'sdate = "17/08/1963"
'result = DateDiff("d",sdate,Date)
'trueage = result / 365
'msgbox Left(trueage,2)
'**********************************
*/

   // output data of each row
  
if ($result->num_rows 0) {
  while(
$row $result->fetch_assoc()) {
   $approved=$row["approved"];
 $lifetime=$row["lifemiles"] + $row["y2013"] + $row["y2014"] + $row["y2015"] + $row["y2016"]+ $row["y2017"]+ $row["y2018"]+ $row["y2019"]+ $row["y2020"];
  
$lifetime=$lifetime $row["y2021"]+ $row["y2022"]+ $row["y2023"]+ $row["y2024"]+ $row["y2025"]+ $row["y2016"]+ $row["y2027"]+ $row["y2028"];
  
//if $row["rider"] <> "" then
if (strlen($row["dob"]) <> 0){
$dob $row["dob"];
$dateOfBirth $dob;
$today date("Y-m-d");
$diff date_diff(date_create($dateOfBirth), date_create($today));
$age $diff->format('%y');

} else {
$age "N/A";
}


If (strlen($row["logupdate"])<> 0){
$logflag=" *";
$fontcolor="black";
if (date_diff(date_create($row["logupdate"]), date_create($today)) < 1) {
$fontcolor="red";
}

}else{ 
$logflag="";
}

// calculate per day and projected
  
$projected=0;
  
$perday=0;
   If (
$row["y2025"]/$dayofyear*365 1){
   
$perday=$row["y2025"]/$dayofyear;
   
$projected=$perday*365;
}
//  color the rolws altermating
   
$reccount $reccount 1;
           
$reccount2 fmod($reccount2);
           switch (
$reccount2){
                case 0:
                $color="#D2DBFF";
break;
                default:
                $color ="#FFFFA8";
   }    


//  1-18 changed     next line to read lifetime instead of lifemiles  (using expr1 value) 
   $regyear=$row["regyear"];
// $lifetime=$row["Expr1"];
 
 
//echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello Dolly!
$ridername=$row["ridername"];
//echo strpos("Hello world!", "world"); // outputs 6
IF (strpos($ridername" ")){
$ridername=str_replace(" ""&"$ridername);
}
// why did I have previousyear for row[2022]
IF ($approved=="YES"){

   ECHO   
"<TR bgcolor="$color "><TD><A Href=update1.php?" $ridername ">" $row["ridername"] .$row["logflag"]. "</a></TD><TD>" $age"</TD><TD> " $row["bike1"]. "</td><TD>" $row["bike2"] ."</TD><TD>" .$row["clubname"]. "</TD><TD>" $row["lifesort"]. "</td><TD>" $row["y2024"] ."</TD><TD>" $row["y2025"] . "</TD><TD>"$row["lastupdate"] . "</TD><TD>" Number_Format($perday) . "</TD><TD>" Number_Format($projected) . "</TD></TR>";

$lft=$lft $row["lifesort"];
//echo $y2024t. "] [";
$y2024t=$y2024t $row["y2024"];
$y2025t=$y2025t $row["y2025"];
}
}
  }
?>

 <TR bgcolor="black">
     <TD><font color="#F27555">TOTALS</font></TD>
<td><font color="#F27555"></font></td>
<td><font color="#F27555"></font></td>
<td><font color="#F27555"></font></td>
  <td><font color="#F27555"></font></td>
     <td><font color="#F27555"><?php ECHO Number_Format($lft,0);?></font></td>
     <td><font color="#F27555"><?php ECHO Number_Format($y2024t,0);?></font></td>
     <td><font color="#F27555"><?php ECHO Number_Format($y2025t,0);?></font></td>
<td></td>
      <td><font color="#F27555"></font></td>
      <td><font color="#F27555"></font></td>
      <!--<TD><font color="#F27555">PACE</font></td>-->

     </tr>
 </table>
<?php
echo mysqli_info($conn);
$conn->close();

?>

<P align="center"><A Href="../">BARROW CREEK</a></p>
<!--<br><br><br><br><br><br><br><br><br><br>
<P><sup>*</sup> <font size="-1"><i>PACE=YTD miles minus 2013 daily average X current<br> day of the year - an approximate comparison
of 2014 pace versus 2013.</i></font></p>-->


<!--Copyright Bob Chappuis -->
</body>
</html>

[edit] adding code tags -Illori

lather

Hey guys, I have it figured out, now working with php 8.1
There  was an error in my database that was ignored by 7.4 but not by the stricter 8.1
Also 8.1 is stricter about assignment of variables and my code caused variable errors.

Thanks again for the help!

Advertisement: