/    Sign up×
Community /Pin to ProfileBookmark

How can I insert arrays into MySQL?

I noticed that WordPress (PHP software) can insert arrays into the database and retrieve it. How can I do the same? Do I have to pass the array through a function.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 28.2011 — serialize() the array before inserting it into the DB, then unserialize() it when you retrieve it.

PS: Don't forget to use the proper escaping mechanism for the DBMS extension being used (e.g. mysql_real_escape_string() for MySQL) [i]after[/i] serializing it before you then insert it.
[code=php]
$string_to_insert = mysql_real_escape_string(serialize($my_array));
$sql = "INSERT INTO table (the_data) VALUES ('$string_to_insert')";
[/code]
×

Success!

Help @linksku spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...