Php 在Mysql中使用数组元素更新整个列
我有一个表userx,如下所示:Php 在Mysql中使用数组元素更新整个列,php,mysql,Php,Mysql,我有一个表userx,如下所示: id----------text---------num 1-----------foo-----------0 2-----------bar-----------0 3-----------widget--------0 4-----------widget--------0 5-----------widget--------0 6-----------widget--------0 7-----------wi
id----------text---------num
1-----------foo-----------0
2-----------bar-----------0
3-----------widget--------0
4-----------widget--------0
5-----------widget--------0
6-----------widget--------0
7-----------widget--------0
8-----------widget--------0
$numeros = array(30, 50, 60, 70, 90, 110, 130, 150);
$query = "UPDATE userx SET num=? WHERE id=?";
for($j = 0, $j < sizeof(numeros); $j++)
{
$index = $j+1;
$stmt = $db->prepare($query);
$stmt->bind_param('ii', $numeros[$j], $index);
$stmt->execute();
}
我有这样一个数组:
id----------text---------num
1-----------foo-----------0
2-----------bar-----------0
3-----------widget--------0
4-----------widget--------0
5-----------widget--------0
6-----------widget--------0
7-----------widget--------0
8-----------widget--------0
$numeros = array(30, 50, 60, 70, 90, 110, 130, 150);
$query = "UPDATE userx SET num=? WHERE id=?";
for($j = 0, $j < sizeof(numeros); $j++)
{
$index = $j+1;
$stmt = $db->prepare($query);
$stmt->bind_param('ii', $numeros[$j], $index);
$stmt->execute();
}
因此,我想进行一个查询,从$numeros获取每个结果,并从id=1 num开始更新到我的列中,如下所示:
id----------text--------------num
1-----------foo--------------30
2-----------bar--------------50
3-----------widget-----------60
4-----------widget-----------70
5-----------widget-----------90
6-----------widget-----------110
7-----------widget-----------130
8-----------widget-----------150
是否可以在循环查询中将更新设置为单个更新或多个更新?怎么做?您可以尝试以下方法:
id----------text---------num
1-----------foo-----------0
2-----------bar-----------0
3-----------widget--------0
4-----------widget--------0
5-----------widget--------0
6-----------widget--------0
7-----------widget--------0
8-----------widget--------0
$numeros = array(30, 50, 60, 70, 90, 110, 130, 150);
$query = "UPDATE userx SET num=? WHERE id=?";
for($j = 0, $j < sizeof(numeros); $j++)
{
$index = $j+1;
$stmt = $db->prepare($query);
$stmt->bind_param('ii', $numeros[$j], $index);
$stmt->execute();
}
因此,虽然从技术上讲,它不是一个查询,但您只需编写一次,您可以尝试类似于使用一个函数通过for循环调用另一个函数来更新每个元素。即
#CODE TO CONNECT TO Mysql database
#then call function to update these column elements
updateColumns();
function updateColumns(){
$query = "SELECT column FROM table";
$result = mysql_query($query);
if(!$result){
echo "Error message you want.</br>";
echo mysql_error();
}
else{
$count = mysql_num_rows($result);
if($count){
# There are rows. now check if there is a match
while($row=mysql_fetch_array($result)){
#cycle through each row of the $result column
foreach($row as $key => $value){
if($key){
updateThisColumnsValue($value);
}
}
}
}
}
}
function updateThisColumnsValue($value){
$query = "UPDATE table SET columnToCHange='newValue' WHERE referenceColumnName = '$value'";
$result = mysql_query($query);
if(!result){
error message
}# that should be it.
}
这里还有一个链接指向sql更新函数
我希望这会有所帮助。但是,您可以在循环中使用准备好的语句,并在数组中循环,每次迭代都绑定不同的值。这是关系数据库的一个大问题,很难提出垂直请求。如果您想这样做,请使用MongoDB和javascript!如果你有一个像他用id做的一样的迭代列,这并不坏。但是是的,如果在id中跳过了一个数字,那么他就完蛋了。是的,条件非常特殊,它可以工作。如果有一种方法,使用一个循环,听起来不错。请告诉我怎么做。我去看看杰夫。Thanks我不确定$j+1是否能在bind_param调用中工作,但我的观点是数组从0开始,id号从1开始,因此您必须以某种方式将$j+1放入其中以获得正确的关联,这不起作用,但我尝试使用for循环解决这个问题,这是我认为的唯一方法。致命错误:对非对象调用成员函数prepare