Monday, 13 July 2015

PHP: HOW TO SAVE MULTIPLE INPUT ENTRIES IN ONE COLUMN IN MYSQL USING PHP?

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 bea: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);

0 comments:

Post a Comment