I'm trying to collect an array of IDs from a MySql DB and update each of them with some values:
global $conn;
$teams = getTeams(); // ID's
$query = "UPDATE t_teams
SET logo = ? WHERE id_team = ?";
foreach($teams as $row) {
$currDir = explode('/', $row["logo"]);
$stmt = $conn->prepare($query);
$stmt->bind_param("si", $currDir[5], $row["id_jogo"]);
$stmt->execute();
}
I'm aware that bind_param
doesn't work like this. There is a bunch of info about this but I can't adapt them to my code.
I think this is something close to what I want: http://php.net/manual/en/mysqli-stmt.bind-param.php#104073
How can I achieve that?
Try changing your statement to use named placeholders like this:
"UPDATE t_teams SET logo = :logo WHERE id_team = :team"
Then bind the values this way:
$stmt->bind_param("logo", $row["logo"]);
$stmt->bind_param("team", $row['id_jogo']);
For some reason, I don't know why, $statement->bindParam()
doesn't work in a loop. You can try using $statement->bindValue()
on PDO, if it's not too much work to switch to PDO of course.
Like so:
global $conn;
$teams = getTeams(); // ID's
$query = "UPDATE t_teams
SET logo = ? WHERE id_team = ?";
$stmt = $conn->prepare($query);
$indexCount = 0;
foreach($teams as $row) {
$currDir = explode('/', $row["logo"]);
$stmt->binValue(++$indexCount, $currDir[5]);
$stmt->execute();
}