I am using PHP to execute a query to a database. I have been successful in executing many queries with the program but one recent one has given me trouble. The original SQL query is as follows:
SELECT Champion, RunnerUp
FROM playoffs
WHERE Champion = "Detroit Red Wings"
AND (RunnerUp = "Montreal Canadiens" OR RunnerUp = "New York Rangers");
This runs successfully and produces the correct output when used in HeidiSQL directly on the database. I tried implementing it in the program as follows, swapping out the actual team names for PHP variables:
$result = mysqli_query($link, "SELECT Champion, RunnerUp
FROM playoffs
WHERE Champion = '$champion'
AND (RunnerUp = '$RunnerUp' OR RunnerUp = '$RunnerUp2')") ;
In this version, it seems that the embedded set of parenthesis is not being identified as such, instead appearing to be part of the quotation. This version produced no output.
I modified it as follows:
$result = mysqli_query($link, "SELECT Champion, RunnerUp
FROM playoffs
WHERE Champion = '$champion'
AND RunnerUp = '$RunnerUp' OR RunnerUp = '$RunnerUp2'") ;
Removing the second set of parenthesis, it executes but does not produce the desired result because the logic reads differently, giving me tuples for whenever $champion and $RunnerUp match or whenever $RunnerUp2 is the runner-up. Is there anyway to execute this statement as I originally intended using PHP?
UPDATE: After putting the query into a variable as follows:
$query = "SELECT Champion, RunnerUp
FROM playoffs
WHERE Champion = '$champion'
AND (RunnerUp = '$RunnerUp' OR RunnerUp = '$RunnerUp2')";
and echoing it, I got the output
SELECT Champion, RunnerUp FROM playoffs WHERE Champion = '' AND (RunnerUp = 'Montreal Canadiens' OR RunnerUp = 'Toronto St. Patricks')
So it looks like for some reason, my champion variable never receives anything. This is because when calling it originally, it was called as 'Champion' instead of 'champion.'