We need some time helps of serialize and unserialize for insert data and retrieve data from database in PHP. Or store date of some value in PHP.
Serialize:
In PHP serialize generates for a storable representation of some values. Serialize is so helpful for storing data or passing data around without losing their type and structure.
You can even have serials () arrays that contain the references themselves. Circular references between arrays / objects will be saved to you serialized.
Unserialize:
Unserialize () accepts a single serialized variable and sends it back to PHP values. It’s use for to make the serialized string into a PHP value again in page.
In this tutorial I will show you how to insert data using serialize and retrieve data using unserialize. Follow the code:
Insert Data with serialize:
<?php if(isset($_POST['submit'])) { $more_text=explode(',',$_POST['more_text']); $more_text=serialize($more_text); $sql=mysqli_query($con,"insert into `Table Name` (`more_text`) values ('".$more_text."')"); } ?>
Retrieve data:
<?php $sql = mysqli_query($con,"SELECT * FROM Table Name LIMIT 1"); $row = mysqli_fetch_assoc($sql); ?> <div class="col-md-12 col-xs-12"> <?php $list = unserialize($row['more_text']); ?> <?php foreach($list as $listval){ ?> <p ><i class="fa fa-chevron-right" aria-hidden="true"></i> <?php echo $listval; ?></p> <?php } ?> </div>