I want to use prepared statements instead of my current code:
if(isset($_POST['submit']) && $_POST['checkGame'] != 'Any')
{
$game = $_POST['checkGame'];
$sql="SELECT ipaddress, port FROM servers WHERE game=('$game')";
$result=mysqli_query($con,$sql);
while ($row=mysqli_fetch_array($result)) {
array_push($serverConnectionArray, ["address" =>$row['ipaddress'], "port" =>$row['port']]);
}
}
I've tried:
if(isset($_POST['submit']) && $_POST['checkGame'] != 'Any')
{
$game = $_POST['checkGame'];
$stmt = $mysqli->prepare("SELECT ipaddress, port FROM servers WHERE game=?");
$stmt->bind_param("s", $game);
$stmt->execute();
$result = $stmt->get_result();
$stmt->fetch();
while ($row=mysqli_fetch_array($result)) {
array_push($serverConnectionArray, ["address" =>$row['ipaddress'], "port" =>$row['port']]);
$stmt->close();
}
}
And more. However, I get this error:
Notice: Undefined variable: mysqli in ...list.php on line 26
Fatal error: Uncaught Error: Call to a member function prepare() on null in ...list.php:26 Stack trace: #0 {main} thrown in ...list.php on line 26
Thanks!