i am new to php pdo and im trying to make a tournamentcms which randomly generates matches by closing registration. i keep getting this error Uncaught Error: Cannot use object of type stdClass as array. Does anyone know how to fix this or know what i did wrong? all help is appreciated.
i've tried switching up fetch methods but did not change anything.
<?php
include 'db.php';
?>
</head>
<?php
if(isset($_GET["id"])){
$stmt = $conn->prepare("SELECT * FROM registration WHERE tournament_id = :id
ORDER BY RAND()");
$stmt->execute([":id" => $_GET["id"]
]);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$values = array();
$amount = count($result);
for($i = 0; $i < $amount; $i += 2)
{
$values[] = '(' . $result[$i]['id'] . ',' . $result[$i + 1]['id'] . ', :tournament_id)';
}
$values = implode(',', $values);
$stmt = $conn->prepare("insert into game(player1, player2, tournament_id) values ($values)");
$stmt->execute([
":tournament_id" => $_GET["id"]
]);
$stmt = $conn->prepare('update tournament set active = 1 where id = :id');
$stmt->execute([
':id' => $_GET['id']
]);
header('Location:index.php');
}
?>
the outcome of this piece of code should be that when i close the registrations for the tournament. registrated players should get inserted to the game table in a random order so matches for the tournament get automatically generated.