我想更新identifier_flag
字段中的以下字词,有没有比我现有的更简单的方法呢?
$temp_query = "select * from faq";
$sql = mysqli_query($connection, $temp_query);
while($row = mysqli_fetch_assoc($sql)){
if(preg_match("/price/", $row['question'])){
$update = "update faq set identifier_flag = 'price' where id = '".$row['id']."'";
mysqli_query($connection, $update);
}
if(preg_match("/colours/", $row['question'])){
$update = "update faq set identifier_flag = 'colours' where id = '".$row['id']."'";
mysqli_query($connection, $update);
}
if(preg_match("/versions/", $row['question'])){
$update = "update faq set identifier_flag = 'versions' where id = '".$row['id']."'";
mysqli_query($connection, $update);
}
if(preg_match("/oil/", $row['question'])){
$update = "update faq set identifier_flag = 'oil' where id = '".$row['id']."'";
mysqli_query($connection, $update);
}
if(preg_match("/seating/", $row['question'])){
$update = "update faq set identifier_flag = 'seating' where id = '".$row['id']."'";
mysqli_query($connection, $update);
}
if(preg_match("/warranty/", $row['question'])){
$update = "update faq set identifier_flag = 'warranty' where id = '".$row['id']."'";
mysqli_query($connection, $update);
}
}
答案 0 :(得分:0)
有很多方法可以做到这一点。 我的大脑会像这样看到它
$temp_query = "select * from faq";
$flags = ['flag1','flag2'];
$sql = mysqli_query($connection,$temp_query);
while($row = mysqli_fetch_assoc($sql)){
foreach($flags as $k=>$v) {
if(preg_match('/'.$v.'/',$row['question'])) {
$update = "update faq set identifier_flag = '{$v}' where id = '".$row['id']."'";
mysqli_query($connection,$update);
//continue; //to go to the next result
}
}
}