我有一个如下所示的数组:
$array = ['184717453','184717454','184717455','184717456','184717457'];
我有一张 table ,还有一个 mobileno
列。某些 mobileno 字段包含 0 值。我想使用上面的数组更新包含 0 值的字段。
使用第一个数组元素将更新包含 0 值的第一个 mobileno 字段,第二个数组元素将更新包含 0 值的第二个 mobileno 字段,依此类推...
到目前为止我已经尝试过:
$array = ['184717453','184717454','184717455','184717456','184717457'];
$clid = count($array);
$sql = mysqli_query($conn,"SELECT * FROM `tbl_subscription` WHERE mobileno = 0");
$rowcount=mysqli_num_rows($sql);
for ($i = 0; $i < $clid; $i++) {
for($j = 0; $j<$rowcount; $j++) {
$sql = "UPDATE `tbl_subscription` SET mobileno='$array[$i]' WHERE mobileno = 0";
$res = mysqli_query($conn, $sql);
}
}
它没有像我想要的那样工作。目前它仅使用第一个数字 184717453
更新所有包含 0 值的字段。我该怎么做,请帮助我。
最佳答案
我已经为你设计了一个解决方案,这里我假设了一个名为 id
的字段,你的表 tbl_subscription
可能有这个字段并且它肯定会起作用,试试下面的代码
<?php
$array = ['184717453','184717454','184717455','184717456','184717457'];
$clid = count($array);
$sql = mysqli_query($conn,"SELECT * FROM `tbl_subscription` WHERE mobileno = 0");
$rowcount=mysqli_num_rows($sql);
for ($i = 0; $i < $clid; $i++) {
$sql = mysqli_query($conn,"SELECT `id` from `tbl_subscription` WHERE mobileno = 0");
if(mysqli_num_rows($sql)>0)
{
$arr=mysql_fetch_array($sql)
$sql = "UPDATE `tbl_subscription` SET mobileno='$array[$i]' WHERE id = ".$arr['id'];
$res = mysqli_query($conn, $sql);
}
}
?>
关于php - 如何只更新表中包含零值的一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27075275/