我需要更新mysql列的初始值,一个令牌列,一开始,这些值都是null,然后,它们需要更新,带有令牌生成函数,怎么做,它只是不更新任何值,请参阅:
$query = "SELECT `id` FROM `acuses_recibo` WHERE `id_envio`=101 AND `token` IS NULL";
$result = mysqli_query($conn, $query);
这里我为特定的id_envio选择那些空标记的id,然后
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);
}
其中getToken(32)
计算长度为32的令牌,这样就不会更新令牌,唯一的更新方式是我将token
中的$queryDos
设置为固定值,我可以通过每个令牌值更新的方式来解决这个问题吗? thanx i.a.
答案 0 :(得分:0)
这就是我解决这个问题的方法,我将问题分为两部分,第一部分只为特定的id
选择所有id_envio
,如下所示
$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);
现在,令牌值更新如下,请注意$indice
必须转换为int类型,因为获取的数组返回字符串
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;
}
}
然后,第二个连接关闭
mysqli_close($connDos);
这就是我可以将令牌值更新为其计算值的方法。我希望它有所帮助。