We are Designers, We are Developers,We are Thinkers,
We are Strategists

How to save multiple input entries in one column in mysql using php?

 
  • 0
3

Introduction:

Serialization method – serialize() is to store multiple fields in single column. PHP is able to automatically serialize most of its variables to strings – letting you save them into storage like $_SESSION. However, there are some tweaks you have to know to avoid exploding .php scripts and performance problems.

serialize() converts an array, given as its only parameter, into a normal string that you can save in a file, pass in a URL, etc. Unserialize() is the opposite of serialize() – it takes a serialized string and converts it back to an array.

Example:

On Post request you have received $_POST data for name, mobile, address, email.

<?php
$data = $_POST;
$data_serialize = serialize($data);
?>

Output:

resulting array will be

a:4:{s:4:"name";s:3:"abc";s:6:"mobile";s:13:"9874325972398";
s:7:"address";s:4:"test";s:5:"email";s:13:"test@test.com";}

use this serialized string to insert into database.

Unserialize value from DB:

$data_unserialize = unserialize($records_serialize);

Leave a Reply