Hi,

I am trying to display some results where one column is equal to another column in a table.

End goal:
display all the results where the round and current round are the same number for every team in the table. the number of teams will change as the number can be added to or deleted.

Here is what I have done so far, however I am getting the error:

Warning: Invalid argument supplied for foreach() in C:\Users\...\index.php on line 34 .... which is line 2 in the code...

$upcomingGames=array();
	$upcomingSql = "SELECT * FROM fixtures_results WHERE `round`==`current_round`";
		foreach ($conn->query($upcomingSql) as $gameRow) {
			$teamURL = str_replace(" ","+",$gameRow['team']);
			$leagueURL = str_replace(" ","+",$gameRow['league']);
			$upcomingGames[]=<<<END
			<tr>
			<td><div class="upcomingGamesLeague"><a href="fixtures.php?leagueName=$leagueURL&teamName=$teamURL">$gameRow[league]:</a></div></td>
			<td><div class="upcomingGamesTeams"><a href="" target="_blank">$gameRow[home_team]</a> V <a href="" target="_blank">$gameRow[away_team]</a></div></td>
			<td><div class="upcomingGamesDate">$gameRow[day] $gameRow[date] $gameRow[time]</a></div></td>
			</tr>
			
END;
		}
	$upcomingGames=implode("\r\n",$upcomingGames);

SO how do I get the results for each team where the round and current_round equal the same number?

thanks for your help.

Cheers,

QWaz

Dani AI

Generated

Nice catch, . In SQL the equality operator is =, and MySQL also provides the NULL-safe equality operator <=>. If you want to match only actual numbers (exclude NULLs), use = and add explicit NULL checks. The double-equals == is not a MySQL comparison operator and can cause the query to fail. See MySQL’s comparison operator list for details. (dev.mysql.com)

Example that matches only teams where both columns are non-NULL and equal:

SELECT league, home_team, away_team, day, date, time
FROM fixtures_results
WHERE `round` = `current_round`
  AND `round` IS NOT NULL
  AND `current_round` IS NOT NULL;

If your intent is to also treat two NULLs as a match, stick with your <=> solution; that is exactly what it is for. (dev.mysql.com)

About the PHP warning: it usually means $conn->query($sql) returned false (syntax error, bad table/column, etc.), and foreach cannot iterate false. Enable exceptions so you see the real SQL error, then iterate the statement:

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $conn->query($sql); // returns PDOStatement or false on failure
foreach ($stmt as $row) {
    // render HTML here, remembering to escape with htmlspecialchars()
}

PDO::query() returns a PDOStatement or false on failure; with ERRMODE_EXCEPTION you will get a clear error message instead of the foreach notice. (php.net)

Small hygiene tips: keep the backticks around round since ROUND() is also a function name; prefer selecting only the columns you render (not *); and HTML-escape values before output to avoid XSS.

Dont worry I solved my own problem.

I needed to use a different Comparison Function in my MySQL Statement:

This worked:

"SELECT * FROM fixtures_results WHERE `round`<=>`current_round`";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.