I just can't get this right. Doing a hobby project as a hobby programmer. Here is a MySQL statement that I'm using on a php web page:
$stmt = $mysqli->prepare("SELECT t1.* FROM laptimes t1
JOIN (
SELECT gamertag, track, vehicle, vehicleclass, platform, controller, setup,
MIN(laptime) AS min_laptime
FROM laptimes
WHERE track LIKE ? AND vehicle LIKE ? AND vehicleclass LIKE ? AND platform
NOT IN ('PS4','XB1') AND controller NOT IN ('Gamepad','Keyboard') AND setup
NOT IN ('Custom')
GROUP BY gamertag, vehicle, vehicleclass
) AS t2 ON t1.gamertag = t2.gamertag AND t1.laptime = t2.min_laptime AND
t1.track = t2.track AND t1.vehicle = t2.vehicle AND t1.vehicleclass =
t2.vehicleclass AND t1.platform = t2.platform AND t1.controller =
t2.controller AND t1.setup = t2.setup
ORDER BY laptime ASC LIMIT ? OFFSET ?");
$stmt->bind_param("sssii", $trackselect, $carselect ,$classelect, $limit, $offset);
$stmt->execute();
Above code, with some hardcoded values, works fine. It gets best laptimes for each player, for a leaderboard. And it excludes results with platform value 'PS4' or 'XB1', controller values 'Gamepad' or 'Keyboard', and setup values 'Custom'. But what I want to do, is replace the hardcoded values in the WHERE clause with variables. I can use ? for track, vehicle, vehicleclass and limit and offset, using bind parameters, no problem. But for platform, controller and setup, I can't use variables, although the code works fine with hardcoded values.
The three NOT IN clauses are excluding records with theses values. In the example, the platform NOT IN ('PS4',XB1')
excludes results with PC or PS4 values in the column 'platform'. Instead of hardcoded values, I want to use $platform, $controller, $setup. I've tried all combinations of curly brackets, parenthesis, back tick, apostrophes, double quotes; nothing seems to work.
If I print the values on for example $platform, it shows: 'PS4','XB1'
(just an example, value is set by checkboxes. So it looks equal to my hardcoded values.
If I replace the hardcoded values with ? (adjusting the bind_param accordingly), nothing gets excluded from the result, even if I tick checkboxes (I print the value of $platform, $controller, $setup and it looks correct).
If I instead do it like this: platform NOT IN ($platform)
then it works. But if I do the same to controller and setup, controller NOT IN ($controller) AND setup NOT IN ($setup)
I get this error message:
Fatal error: Call to a member function bind_param() on a non-object
pointing to the line with bind_param. I have adjusted the bind_param line to correct number of "sss" and $variables.
So how can I use variables instead of hardcoded values?
I'm way out of my league here, I can't understand it. Printed variable values are exactly like the hardcoded examples. Do I need a special kind of parenthesis, or some kind of magic sauce? I don't know. Hopefully I've explained the issue well enough. If not, let me know and I'll try again.