My webpages have died with php8 server upgrade

a few months ago my free server updated its php to php8 and now nothing on my results website shows up when you click a link.

http://trotdata.byethost32.com/

im assuming i need to update the php code for each serch page but ive no idea where to start and what to do. at the moment i have the following for the results search page which currently returns a blank page.

{

$Meetcode = $_GET['Meetcode'];

}

{

$searchSQL = "SELECT RaceDate, RaceNumber, RaceName, RaceDist, Track, Placing, Sorter, Horseid, Trail, Draw, Driver, Dist, Time, Comment, Prize FROM RaceTable WHERE ";

$types = array();

$types[] = $_GET['Meetcode']?"`Meetcode` LIKE '$Meetcode'":'';

$types = array_filter($types, "removeEmpty");

if (count($types) < 1)

$types[] = "`Meetcode` LIKE '$Meetcode'";

$andOr = isset($_GET['matchall'])?'AND':'OR';

$searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY racedate, racenumber, Sorter, Placing ASC"; // order by title.order by title.

$searchResult = mysqli_query($searchSQL) or trigger_error("There was an error.<br/>" . mysqli_error() . "<br />SQL Was: {$searchSQL}");

if (mysqli_num_rows($searchResult) < 1)

{

$error[] = "The search term provided {$searchTerms} yielded no results.";

}

else

{

$results = array(); // the result array

echo '<table border="0" align=center width=80%>';

$last_race_number ='';

while ($row = mysqli_fetch_assoc($searchResult))

{

if($last_race_number != $row['RaceNumber'])

{

echo "<tr><th colspan='9'>". $row['RaceName'] ." - ". $row['RaceDist']." - ". $row['Track']." - ". $row['RaceDate'] ."</td></tr>";

}

echo '<tr>';

echo "<th>".$row['Placing']."</td>";

echo "<td align=left><a href='horse.html.php?Horseid=".urlencode($row['Horseid'])."'>".$row['Horseid']."</a></td>";

echo "<td>".$row['Trail']."</td>";

echo "<td>".$row['Draw']."</td>";

echo "<td align=left>".$row['Driver']."</td>";

echo "<td>".$row['Dist']."</td>";

echo "<td>".$row['Time']."</td>";

echo "<td align=left>".$row['Comment']."</td>";

echo "<td>£".$row['Prize']."</td>";

echo '</tr>';

$last_race_number = $row['RaceNumber'];

}

echo '</table>';

}

}

?>

************when i test the code i get an error for line=

$searchResult = mysqli_query($searchSQL) or trigger_error(“There was an error.
” . mysqli_error() . “
SQL Was: {$searchSQL}”);

and line=

$error[] = “The search term provided {$searchTerms} yielded no results.”;

what do i need to do to ressurect my site please??

You would implement the steps I gave in a detailed reply in the other php help forum.

Here’s your existing code with comments added -

<?php

// the following logic is either incomplete/altered or is broken
// you should trim, then validate all inputs before using them
{
	$Meetcode = $_GET['Meetcode'];
}

{

$searchSQL = "SELECT RaceDate, RaceNumber, RaceName, RaceDist, Track, Placing, Sorter,
 Horseid, Trail, Draw, Driver, Dist, Time, Comment, Prize
 FROM RaceTable WHERE ";

// the following logic for $types makes no sense and is either incomplete/altered or doesn't do what you think
// to convert this to a prepared query, you would create an array for the prepared query input parameters, and every place you add an element to $types, which would use a ? place-holder instead of the value, you would add the value to the parameter array
$types = array();

// if the 'Metcode' get input is true, add this term to the query
// a LIKE comparison is typically used with wild-card match characters. otherwise just use an = comparison
$types[] = $_GET['Meetcode'] ? "`Meetcode` LIKE '$Meetcode'" : '';

// remove any empty entries
// the posted code isn't capable of adding empty entries.
$types = array_filter($types, "removeEmpty");

// if empty
if (count($types) < 1)
// if this is empty, there's nothing in $Meetcode in the posted code and putting this back into $types doesn't make any sense
$types[] = "`Meetcode` LIKE '$Meetcode'";

// if matchall isset, use AND, otherwise use OR
$andOr = isset($_GET['matchall']) ? 'AND' : 'OR';

$searchSQL .= implode(" {$andOr} ", $types) .
 " ORDER BY racedate, racenumber, Sorter, Placing ASC"; // order by title.order by title.

 // if using a prepared query, you would prepare it here, then execute it with the array of input parameters
$searchResult = mysqli_query($searchSQL) or trigger_error("There was an error.<br/>" . mysqli_error() . "<br />SQL Was: {$searchSQL}");

// if using the PDO extension, you would typically just fetch all the data into an array variable, then test and loop over that variable to produce the output.
if (mysqli_num_rows($searchResult) < 1)
{
	// wondering where $searchTerms is defined
	$error[] = "The search term provided {$searchTerms} yielded no results.";
}
else
{
	// producing this type of output is easier if you index/pivot the data when you fetch it, using the RaceNumber as the main array index, then loop over the indexed data
	// the following variable/array isn't used in the code
	$results = array(); // the result array
	// you need to validate the resulting web page at validator.w3.org
	echo '<table border="0" align=center width=80%>';
	$last_race_number ='';

	while ($row = mysqli_fetch_assoc($searchResult))
	{
		if($last_race_number != $row['RaceNumber'])
		{
			// you can put php variables inside double-quoted strings without using concatenation
			echo "<tr><th colspan='9'>". $row['RaceName'] ." - ". $row['RaceDist']." - ". $row['Track']." - ". $row['RaceDate'] ."</td></tr>";
		}
		echo '<tr>';
		echo "<th>".$row['Placing']."</td>";
		// you should build the query string part of urls by including any existing get parameters, so that you don't loose the search terms when you goto the linked page
		// you can use php's http_build_query() to help you do this
		echo "<td align=left><a href='horse.html.php?Horseid=".urlencode($row['Horseid'])."'>".$row['Horseid']."</a></td>";
		echo "<td>".$row['Trail']."</td>";
		echo "<td>".$row['Draw']."</td>";
		echo "<td align=left>".$row['Driver']."</td>";
		echo "<td>".$row['Dist']."</td>";
		echo "<td>".$row['Time']."</td>";
		echo "<td align=left>".$row['Comment']."</td>";
		echo "<td>£".$row['Prize']."</td>";
		echo '</tr>';

		$last_race_number = $row['RaceNumber'];
	}
	echo '</table>';
}
}
?>

If converting this to use the PDO extension and prepared queries, there’s three comments in the code indicating what changes need to be made.

thank you very much for your reply, its like hammering mud into a stone im afraid getting my head to understand all this. there are so many words and phrases that im sure make sense to people who understand this php stuff but to passing mortals who only want to use this once its baffling!

the 1st 2 lines of the reply are no doubt very simple but even that has me floored

“you should trim, then validate all inputs”

trim, ok so edit something somehow, and then validate the code by some means using something. im a total muppet sorry.

theres then a bit about $types (im so thick i dont even know which button on the key board gives me the proper string character thing)… i did the php script several years ago (cabbled it together with no real understanding what i was doing) since which ive never thought about a sting and array or a whatever again so ive forgotten mostly all the little i knew.

all i know is it all worked somehow before the server went to php8 and i was hoping that somewhere there was a step by step guide as to what i have to do to ammend it to the new modern whatsit of doing things but it all seems like a massive going on and having to spend ages learning a new language simply to get me back to exactly where i was this time last year.

The posted code contains undefined variables, variables that are defined but not used, stray pairs of {} (which php should not allow, but does), logic tests that won’t ever be true, … This appears to be the code that accepts the meetcode input from the links? What is the complete code for that page, less any database connection credentals?

Next, there needs to be some definition of what the input search data is and what to do if there is and is not any input. If the Meetcode input is empty, do you want to match all Meetcodes or display a message that you must select/enter a Meetcode? Is there actually a matchall input? The use of a LIKE comparison typically uses wild-card match characters, e.g. %, _. Are the Meetcode values something that a partial match gives meaningful results? If not, just use an = comparison to match exact values.

Based on what I have seen on the site, here is what you should do -

  1. Have a year select/option menu, listing the years data is available for, default to the current year.
  2. Query for and display the meet results for the selected year.
  3. When a result/meetcode link is clicked, query for and display the results for that meetcode.
1 Like

thank you very much for your reply i will try and answer the questions. firstly yeas it is. the meetings are all listed on the home page and the links contain the meetcode for that meeting as so:
(i killed this link as it would not let me post it)**
…com/meeting.html.php?Meetcode=tp12823

that ten goes to a html page which is:

<html><head>

  
  <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">

  
  <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>Trotdata Results Database</title></head><body style="font-family: times; text-align: center; color: rgb(0, 0, 0); background-color: rgb(224, 224, 224);" alink="#cc0000" link="#660000" vlink="#ff0000">
<div style="text-align: center; color: rgb(0, 0, 255);"><big><big><big>TROTDATA</big></big></big><br>
ONLINE DATABASE OF UK &amp; IRELAND TROTTING RESULTS<br>
<a href="http://trotdata.byethost32.com">Home Page</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="http://trotdata.byethost32.com/search.html.php">New Search</a><br>
</div>
 <div style="font-family: arial; color: rgb(0, 0, 0);"
 <br>
 <br>
 <big><big>Meeting Results</big></big>
<br>
Distance column shows winning distance or cumulative distance beaten for non winners. 
<br> 
Final column shows prize money won in either GBP or EUR as appropriate.
<br><br>

<?php include 'meetingres.php'; ?> </div>

</body></html>

which then includes the php file above which should find every horse which ran at that meeting sorted by race number and then placing in that race. the code for that minus the database connection stuff is what i put in my 1st post. the input can never be empty so all that can be removed its not needed. that code is always defined by the link. it ended up there as i have the same code for the ‘search’ bit of the site where you can search for individual horses full results by name of horse in a search box.

It will be easier to just post an example for you to examine/copy, than to try to explain the meaning of all points given.

The code for any page should be laid out in this general order -

  1. initialization
  2. post method form processing
  3. get method business logic - get/produce data needed to display the page
  4. html document

For the current operation, of getting and displaying matching Meetcode data, it would look like this (tested with some faked query data, but should work with your database) -

<?php

// this is apparently meeting.html.php
// the only input from the yearly display page(s) is $_GET['Meetcode'] and it is required

// while it would be desirable to have one common search/result page, it would require knowing all the input possibilities to do so

// 1. initialization
// note: this code uses the PDO extension, prepared queries when suppling values to a query when it gets executed, and exceptions for database statement error handling
require 'pdo_connection.php';

// 2. post method form processing

// 3. get method business logic - get/produce data needed to display the page

// condition the meetcode input, default to an empty value
$meet_code = trim($_GET['Meetcode'] ?? '');

// get meet data matching the submitted meetcode
if($meet_code)
{
	// note: the 1st column in the select list is being used to index/pivot the data when it gets fetched, i.e. this breaks the data into groups by that value
	// note: you don't need to select columns that are only used in the query, e.g. sorter
	$sql = "SELECT RaceNumber, RaceDate, RaceName, RaceDist, Track, Placing,
	Horseid, Trail, Draw, Driver, Dist, Time, Comment, Prize
	FROM RaceTable
	WHERE Meetcode = ?
	ORDER BY racedate, racenumber, Sorter, Placing"; // default ORDER BY is ASC, so it is not necessary to list ASC

	$stmt = $pdo->prepare($sql);
	$stmt->execute([$meet_code]);
	// fetch all the data, indexing/pivoting it by the 1st column in the select list - RaceNumber
	$meet_data = $stmt->fetchAll(PDO::FETCH_GROUP);
}

// 4. html document
// note: this is incomplete and only shows the dynamic parts produced by php
?>

<?php
// produce the meet output
if(empty($meet_data))
{
	echo "<p>The search term yielded no results.</p>";
	// if you see this error, either there was was no Meetcode or it didn't match any data
}
else
{
	// you need to validate the resulting web page at validator.w3.org
	echo '<table border="0" align=center width=80%>';
	foreach($meet_data as $race_number=>$arr)
	{
		// get the heading data from the zeroth row
		$row = $arr[0];
		// you can put php variables inside double-quoted strings without using concatenation
		echo "<tr><th colspan='9'>{$row['RaceName']} - {$row['RaceDist']} - {$row['Track']} - {$row['RaceDate']}</td></tr>\n";
		
		// loop over the $arr of data for the current race
		// get a copy of any existing $_GET parameters, e.g. any meetcode/search values
		$get = $_GET;
		foreach($arr as $row)
		{
			echo '<tr>';
			echo "<td>{$row['Placing']}</td>";
			// build the query string part of the url
			$get['Horseid'] = $row['Horseid'];
			$qs = http_build_query($get,'','&amp;'); // note: this urlencodes the data for you
			echo "<td align=left><a href='horse.html.php?$qs'>{$row['Horseid']}</a></td>";
			echo "<td>{$row['Trail']}</td>";
			echo "<td>{$row['Draw']}</td>";
			echo "<td align=left>{$row['Driver']}</td>";
			echo "<td>{$row['Dist']}</td>";
			echo "<td>{$row['Time']}</td>";
			echo "<td align=left>{$row['Comment']}</td>";
			echo "<td>£{$row['Prize']}</td>";
			echo "</tr>\n";
		}
	}
	echo '</table>';
}
?>

Here is typical PDO connection code -

<?php
$DB_HOST = ''; // database host name or ip address
$DB_USER = ''; // database username
$DB_PASS = ''; // database password
$DB_NAME = ''; // database name
$DB_ENCODING = 'utf8mb4'; // db character encoding. set to match your database table's character set

$options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // set the error mode to exceptions. this is the default now in php8+
			PDO::ATTR_EMULATE_PREPARES => false, // run real prepared queries
			PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC // set default fetch mode to assoc
			];

$pdo = new pdo("mysql:host=$DB_HOST;dbname=$DB_NAME;charset=$DB_ENCODING",$DB_USER,$DB_PASS,$options);
1 Like

It appears that the yearly display pages are hard-coded html? These should be dynamically produced based on database data. They will then automatically update as new data is inserted, without needing to create/edit page(s).

Here is example code to dynamically produce the yearly display page (not fully tested but should work) -

<?php

// 1. initialization
require 'pdo_connection.php';

// 2. post method form processing

// 3. get method business logic - get/produce data needed to display the page

// get the years that data is available
$sql = "SELECT DISTINCT YEAR(RaceDate) year
	FROM RaceTable
	ORDER BY year DESC";
$stmt = $pdo->query($sql);
$available_years = $stmt->fetchAll(PDO::FETCH_COLUMN);

// default to the current year
if(empty($_GET['year']))
{
	$_GET['year'] = date('Y');
}

// condition the year input
$year = trim($_GET['year']);

// get the data for the selected year
// there are apparently values that indicate canceled, abandoned, and results not yet published, ...
$sql = "SELECT MONTHNAME(RaceDate) monthname, DATE_FORMAT(RaceDate,'%a') dayofweek,
 DATE_FORMAT(RaceDate,'%D') dayofmonth, Track, Meetcode
	FROM RaceTable
	WHERE YEAR(RaceDate) = ?
	ORDER BY RaceDate";
$stmt = $pdo->prepare($sql);
$stmt->execute([$year]);
// fetch the data, indexed by the 1st column in the SELECT list - monthname
$year_data = $stmt->fetchAll(PDO::FETCH_GROUP);

// 4. html document
// note: this is incomplete and only shows the dynamic parts produced by php
?>

<?php
// output year select/option menu
?>
<form>
<label>Year: <select name='year' onchange='this.form.submit();'>
<option value="">Select Year</option>
<?php
foreach($available_years as $choice)
{
	$sel = $year == $choice ? 'selected' : '';
	echo "<option value='$choice' $sel>$choice</option>";
}
?>
</select></label>
<noscript><input type='submit'></noscript>
</form>

<?php
// output the yearly data
// for this example, this is just output in a single column, separated by the month name
if(empty($year_data))
{
	echo "<p>There is no data for the selected year - $year</p>";
	// note: since the available year selection is based on the actual data, if you see this error it is due to a programming mistake
}
else
{	
	// get a copy of any existing $_GET parameters - year/search
	$get = $_GET;
	foreach($year_data as $month=>$arr)
	{
		$month = strtoupper($month);
		echo "<p>$month</p>";
		foreach($arr as $row)
		{
			// dayofweek, dayofmonth, Track, Meetcode link or canceled/abandoned/result not yet published text
			// determine if there is a RESULTS link or text display
			// if there is a Meetcode, produce the link with any existing get parameters so that the year selection carries through to the meet display page
			// since i don't know if/what data indicates the text values, this example will just produce the link if there is a true Meetcode
			$result = '- no result';
			if($row['Meetcode'])
			{
				// build the query string part of the url
				$get['Meetcode'] = $row['Meetcode'];
				$qs = http_build_query($get,'','&amp;'); // note: this urlencodes the data for you
				$result = "- <a href='meeting.html.php?$qs'>RESULT</a>";
			}
			echo "<p>{$row['dayofweek']} {$row['dayofmonth']} {$row['Track']} $result</p>";
		}
	}
}
?>

This doesn’t have any code to deal with the canceled, abandoned, and results not yet published, … text, assuming this is even stored in the database table.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service