I need to update the initial values of a mysql column, a tokens column, at the beginning, those values are all null, then, they need to be updated, with a tokens generating function, how am doing it, it just doesn´t updates any values, see:
$query = "SELECT `id` FROM `acuses_recibo` WHERE `id_envio`=101 AND `token` IS NULL";
$result = mysqli_query($conn, $query);
here I select the id´s of those null tokens for an specific id_envio, then
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){//as long as there are token null values
foreach($row as $id=>$row['id']){
$indice = $row['id'];
$v=getToken(32);
echo $v.PHP_EOL;
echo "id es: ".$row['id'].PHP_EOL;
$queryDos = "UPDATE `acuses_recibo` SET `token`={$v} WHERE `id`={$indice} ";
mysqli_query($conn, $queryDos);
}
where getToken(32)
calculates a token of length 32, this way no token is updated, the only way they get updated is if I set token
in $queryDos
to a fixed value, how could I remedy this in such a way that every token value gets updated? thanx i.a.
This is how I solved it, I separated the problem in two parts, the first just selects all the id
´s for a specific id_envio
as follows
$query = "SELECT `id` FROM `acuses_recibo` WHERE `id_envio`=101 AND `token` IS NULL";
$result = mysqli_query($conn, $query);
// an arrays is created and used to store the id´s selected
$members = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$members[] = array('id'=> $row['id']);
}
// the connection is then closed, something I found on mysql not being explicitly multiconnection
mysqli_close($conn);
// then a new connection is setup
$connDos = mysqli_connect($servername, $username, $password, $database);
Now, the token values are updated as follows, please note that $indice
has to be cast to int type because the array fetched returns strings
foreach($members as $m){
$indice = $m["id"];
$indice = intval($indice);
$voken=getToken(32);
$queryDos = " UPDATE `acuses_recibo` SET `token`='{$voken}' WHERE `id`='{$indice}' ";
if (mysqli_query($connDos, $queryDos)) {
echo "Record updated successfully".PHP_EOL;
} else {
echo "Error updating record: " . mysqli_error($connDos).PHP_EOL;
}
}
then, the second connection is closed
mysqli_close($connDos);
and this is how I could update the token values to their calculated values. I hope it helps somebody.