Insert array data into database column with mysql

0.3K views
no comments
6 Jan 2015 11:55 pm

Many times we need to store #array in #database with because may we don’t want to create all specified fields with names in #database table, so we just inserted, and here is example that I want to save whole #array data in database table field mysql.

[php]
$save_array = array(
‘sd_sitename’ => ‘SDTuts’
,’sd_blogurl’ => ‘http://www.sdtuts.com’
,’sd_author’ => ‘Rameez Soomro(Faisal Ahsan)’
,’sd_post_name’ => ‘Insert array data into database column with mysql’
,’sd_script_name’ => ‘php’
,’sd_cms_name’ => ‘WordPress’
);
[/php]

mysql cannot allow you to save array like above array style, there are 2 method to store a data in database table column

1. json

SAVE WITH JSON

[php]
$save_array = json_encode($save_array);
// RUN MYSQL QUERY INSERT OR UPDATE
[/php]

RETRIEVE WITH JSON

[php]
///RUN A MYSQL QUERY TO GET COLUMN WHERE WE STORED JSON DATA
$saved_array = json_decode[‘saved_json_data’];

[/php]

2. serialize

SAVE WITH SERIALIZE

[php]
$save_in_db  = serialize($save_array);
//RUN MYSQL INSERT QUERY
[/php]

RETRIEVE WITH

[php]
//RUN QUERY TO GET SAVE DATA FROM TABLE
$save_in_db  = unserialize($retrieved_data[‘my_saved_data_serialize_format’]);
[/php]

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>