Answers for "php insert array serialize and unserialize"

PHP
0

php insert array serialize and unserialize

<?php

include "config.php";

// Indexed Array
$names_arr = array("Yogesh singh","Sonarika Bhadoria","Vijay Maurya");

// Associative Array
$users_arr[] = array("username"=>"yssyogesh","name"=>"Yogesh singh");
$users_arr[] = array("username"=>"bsonarika","name"=>"Sonarika Bhadoria");
$users_arr[] = array("username"=>"vijay","name"=>"Vijay Maurya");

// Serialize the Array
$names_str = serialize($names_arr);
$users_str = serialize($users_arr);

// Insert record
$sql = "INSERT INTO contents_arr(arr_serialize1,arr_serialize2) VALUES('".$names_str."','".$users_str."')";
mysqli_query($con,$sql); 

// Read record
$sql = mysqli_query($con,"SELECT * FROM contents_arr");
while($row = mysqli_fetch_assoc($sql)){
   
   // Unserialize
   $arr_unserialize1 = unserialize($row['arr_serialize1']);
   $arr_unserialize2 = unserialize($row['arr_serialize2']);
   
   // Display
   echo "<pre>";
   print_r($arr_unserialize1);
   print_r($arr_unserialize2);
   echo "</pre>";
}
Posted by: Guest on October-11-2021

Browse Popular Code Answers by Language