Delete a specific ID entered as serialize in a column in MySQL

0

In my db, I have a column called Range_ids , which inserts IDs using the serialize function. These IDs stay as an array, the problem is how I can make a DELETE of the specific ID within the column.

//Usando a função serialize eu insiro esses ids 1052,1053..... em serie

+------------+----------+--------------------------------------------+
| id         | nome     | Range_ids                                  |
+------------+----------+--------------------------------------------+
| 01         | Prod1    | a:2:{i:1052;s:4:"1052";i:1053;s:4:"1053";} |
| 02         | Prod2    | a:2:{i:1052;s:4:"1052";i:1053;s:4:"1053";} |
| 03         | Prod3    | a:2:{i:1052;s:4:"1052";i:1053;s:4:"1053";} |
+------------+----------+--------------------------------------------+

The problem is how do I delete a specific ID on the particular line. Example: I want to remove the number 1052 from column range_id on line 1

//Precisaria de Algo assim
<a href="del.php?id_range=1052">Deletar</a>
    
asked by anonymous 23.10.2015 / 03:50

1 answer

0

You would have to do the following:

<?php
$num = "1052";
$sql = "SELECT range_ids FROM tabela";
if($query = $conn->query($sql)){
while($row = $query->fetch_assoc()){
    $linha[] = $row["range_ids"];
}
}
//var_dump($linha);
$db_range = $linha;
if(is_array($db_range)){
    foreach($db_range as $range){
        $un_dbrange = unserialize($range);
        //var_dump($un_dbrange);
        foreach($un_dbrange as $key=>$value){
            if($value == $num){
                unset($un_dbrange[$key]);
            }
        }
    }
}
echo "<hr/>";
var_dump($un_dbrange);
?>

Do not you see how beautiful it is?

In case, to delete a specific line, you would also have to force the id of the table, but would still be many instructions. At the moment I do not know why you're working with a serialized field, but it's not usually recommended to work with fields like this because they're hard to handle, and it forces us to create too many routines. p>     

23.10.2015 / 04:49