PDA

View Full Version : storing arrays


sabret00the
03-27-2006, 06:41 PM
is there a superior method to serialize?

can i just store an array straight into a db =

INSERT INTO table(col1, col2)
VALUES ('hello', 'array([greet]=>hello, [bye]=>bye)')

Code Monkey
03-28-2006, 02:18 AM
No, you need to serialize it. the process to get the query data returned and evaled as php code again would be more costly than serialize. Serialize is not as efficient as it could be, but it's not the devil everyone makes it out to be either. It's a useful tool that serves a purpose.

Paul M
03-28-2006, 02:27 AM
If it's text data you could implode it into a string (with a suitable seperator) and explode it when you pull it from the db - but I'm not sure this method would be any better.

sabret00the
03-28-2006, 10:21 AM
thanks guys, i decided on the serialize in the late/early hours of last night, but just to give you an idea of what i'm doing

INSERT INTO ej_moodset(title, author, location, moods)
VALUES
('Hamsters', 'r2d2', 'images/ej/moods/r2d2__hamsters', 'a:127:{s:11:"Acomplished";s:15:"Acomplished.gif";s:10:"Aggrivated";s:14:"Aggrivated.gif";s:6:"Amused";s:10:"Amused.gif";s:5:"Angry";s:9:"Angry.gif";s:9:"Apethetic";s:13:"Apethetic.gif";s:8:"Artistic";s:12:"Artistic.gif";s:5:"Awake";s:9:"Awake.gif";s:6:"+++++y";s:10:"+++++y.gif";s:4:"Blah";s:8:"Blah.gif";s:5:"Blank";s:9:"Blank.gif";s:5:"Bored";s:9:"Bored.gif";s:6:"Bouncy";s:10:"Bouncy.gif";s:4:"Busy";s:8:"Busy.gif";s:4:"Calm";s:8:"Calm.gif";s:9:"Cheerfull";s:13:"Cheerfull.gif";s:7:"Chipper";s:11:"Chipper.gif";s:4:"Cold";s:8:"Cold.gif";s:10:"Complacent";s:14:"Complacent.gif";s:8:"Confused";s:12:"Confused.gif";s:13:"Contemplative";s:17:"Contemplative.gif";s:7:"Content";s:11:"Content.gif";s:6:"Cranky";s:10:"Cranky.gif";s:6:"Crappy";s:10:"Crappy.gif";s:5:"Crazy";s:9:"Crazy.gif";s:8:"Creative";s:12:"Creative.gif";s:7:"Crushed";s:11:"Crushed.gif";s:7:"Curious";s:11:"Curious.gif";s:7:"Cynical";s:11:"Cynical.gif";s:9:"Depressed";s:13:"Depressed.gif";s:10:"Determined";s:14:"Determined.gif";s:7:"Devious";s:11:"Devious.gif";s:5:"Dirty";s:9:"Dirty.gif";s:10:"Discontent";s:14:"Discontent.gif";s:13:"Dissappointed";s:17:"Dissappointed.gif";s:10:"Distressed";s:14:"Distressed.gif";s:5:"Ditzy";s:9:"Ditzy.gif";s:5:"Dorky";s:9:"Dorky.gif";s:7:"Drained";s:11:"Drained.gif";s:5:"Drunk";s:9:"Drunk.gif";s:10:"Embaressed";s:14:"Embaressed.gif";s:9:"energetic";s:13:"energetic.gif";s:7:"Enraged";s:11:"Enraged.gif";s:10:"Enthralled";s:14:"Enthralled.gif";s:7:"Envious";s:11:"Envious.gif";s:7:"Estatic";s:11:"Estatic.gif";s:9:"Exhausted";s:13:"Exhausted.gif";s:6:"Flirty";s:10:"Flirty.gif";s:4:"Full";s:8:"Full.gif";s:9:"Fustrated";s:13:"Fustrated.gif";s:5:"Geeky";s:9:"Geeky.gif";s:5:"Giddy";s:9:"Giddy.gif";s:6:"Giggly";s:10:"Giggly.gif";s:6:"Gloomy";s:10:"Gloomy.gif";s:4:"Good";s:8:"Good.gif";s:9:"gratefull";s:13:"gratefull.gif";s:6:"Groggy";s:10:"Groggy.gif";s:6:"Grumpy";s:10:"Grumpy.gif";s:6:"Guilty";s:10:"Guilty.gif";s:5:"Happy";s:9:"Happy.gif";s:4:"High";s:8:"High.gif";s:8:"Hopefull";s:12:"Hopefull.gif";s:5:"Horny";s:9:"Horny.gif";s:3:"Hot";s:7:"Hot.gif";s:6:"Hungry";s:10:"Hungry.gif";s:5:"Hyper";s:9:"Hyper.gif";s:9:"Impressed";s:13:"Impressed.gif";s:13:"Indescribable";s:17:"Indescribable.gif";s:11:"Indifferent";s:15:"Indifferent.gif";s:10:"infuriated";s:14:"infuriated.gif";s:11:"Intimidated";s:15:"Intimidated.gif";s:5:"Irate";s:9:"Irate.gif";s:9:"Irritated";s:13:"Irritated.gif";s:7:"Jealous";s:11:"Jealous.gif";s:8:"Jubilant";s:12:"Jubilant.gif";s:4:"Lazy";s:8:"Lazy.gif";s:8:"Listless";s:12:"Listless.gif";s:6:"Lonely";s:10:"Lonely.gif";s:5:"Loved";s:9:"Loved.gif";s:10:"Melancholy";s:14:"Melancholy.gif";s:6:"Mellow";s:10:"Mellow.gif";s:11:"Mischevious";s:15:"Mischevious.gif";s:5:"Moody";s:9:"Moody.gif";s:6:"Morose";s:10:"Morose.gif";s:7:"Naughty";s:11:"Naughty.gif";s:9:"Nausiated";s:13:"Nausiated.gif";s:5:"Nerdy";s:9:"Nerdy.gif";s:7:"Nervous";s:11:"Nervous.gif";s:9:"Nostalgic";s:13:"Nostalgic.gif";s:4:"Numb";s:8:"Numb.gif";s:2:"Ok";s:6:"Ok.gif";s:10:"optimistic";s:14:"optimistic.gif";s:9:"Peacefull";s:13:"Peacefull.gif";s:7:"Pensive";s:11:"Pensive.gif";s:11:"Pessimistic";s:15:"Pessimistic.gif";s:10:"Pissed_Off";s:14:"Pissed_Off.gif";s:7:"Pleased";s:11:"Pleased.gif";s:9:"Predetary";s:13:"Predetary.gif";s:10:"Productive";s:14:"Productive.gif";s:8:"Quixotic";s:12:"Quixotic.gif";s:9:"Recumbent";s:13:"Recumbent.gif";s:9:"Refreshed";s:13:"Refreshed.gif";s:8:"Rejected";s:12:"Rejected.gif";s:11:"Rejuvinated";s:15:"Rejuvinated.gif";s:7:"Relaxed";s:11:"Relaxed.gif";s:7:"Relived";s:11:"Relived.gif";s:8:"Restless";s:12:"Restless.gif";s:6:"Rushed";s:10:"Rushed.gif";s:3:"Sad";s:7:"Sad.gif";s:9:"Satisfied";s:13:"Satisfied.gif";s:6:"Scared";s:10:"Scared.gif";s:7:"Shocked";s:11:"Shocked.gif";s:4:"Sick";s:8:"Sick.gif";s:5:"Silly";s:9:"Silly.gif";s:6:"Sleepy";s:10:"Sleepy.gif";s:4:"Sore";s:8:"Sore.gif";s:8:"Stressed";s:12:"Stressed.gif";s:9:"Surprised";s:13:"Surprised.gif";s:11:"Sympathetic";s:15:"Sympathetic.gif";s:9:"thankfull";s:13:"thankfull.gif";s:7:"Thirsty";s:11:"Thirsty.gif";s:11:"Thoughtfull";s:15:"Thoughtfull.gif";s:5:"Tired";s:9:"Tired.gif";s:7:"Touched";s:11:"Touched.gif";s:13:"uncomfortable";s:17:"uncomfortable.gif";s:5:"Wierd";s:9:"Wierd.gif";s:7:"Working";s:11:"Working.gif";s:7:"Worried";s:11:"Worried.gif";}')

the query consists of like 17 of those, 35,000 characters long but only 0.0117 seconds to execute.

however retreival goes a little something like this
function construct_mood($moodsetid)
{
global $vbulletin;

$mood_options = $vbulletin->db->query_first("SELECT moods FROM ej_moodset WHERE moodsetid = " . intval($moodsetid) . "");

$mood_options = unserialize($mood_options['moods']);

$mood_drop = '<select name="current_mood" onChange="changeSelectImg(this);"><option value=""></option>';

foreach ($mood_options AS $key => $value)
{
$mood_drop .= '<option value="' . $value . '">' . $key . '</option>';
}

$mood_drop .= '</select>';

return $mood_drop;
}
which is why i was worrying about getting the serialize.