RE: [PHP] Re: storing array in mysql
| From: | Warren Vail | Date: | Tue, 31 Jul 2001 14:38:58 +0000 |
| Subject: | RE: [PHP] Re: storing array in mysql | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-60421@lists.php.net to get a copy of this message | ||
I never seem to be lucky enough to be sure of the type of data stored in a
php array, since php handles mixtures of types so forgivingly, and because
most of my data comes from forms, with users key in what they like,
including double and single quotes, parentheses (and especially commas, how
do you prevent breaking up your array and putting it back together with a
different row count because someone keyed in a comma?), etc. I would think
you would have to go to a lot of trouble to make sure that an array contains
only numeric data, or only strings that did not contain problem causing
characters.
You are right about more space being required for serialize, I often have to
resort to TEXT data types to provide enough space in the column for data
(65k runs out fast), and that is a bit slower as well.
Warren
-----Original Message-----
From: elias [mailto:elias_bachaalany@yahoo.com]
Sent: Tuesday, July 31, 2001 8:09 AM
To: php-general@lists.php.net
Subject: Re: [PHP] Re: storing array in mysql
Yes true, you can use serialize.
But since you know the format of your $array variable (which is simply
holding one data type) you can safely use split() and join()
better and smaller when stored in that field because they are comma
seperated.
"Warren Vail" <warren@availabletech.com> wrote in message
news:001701c119c8$562b0ca0$b5887ed8@nicker...
> What I have used to store an array in mysql is;
>
> $value = addslashes(serialize($array));
> $query = "INSERT INTO table (column) VALUES (\"$value\")"
>
> and upon retrieval
> $query = "SELECT column FROM table";
> .....
> while($row = mysql_fetch_array($result)) {
> $value = unserialize(stripslashes($row["column"]));
> }
>
> Note: serialize allows me to store the array in a single column and
> addslashes makes the data mysql safe (i.e. allows me to store quotes in
the
> column, just in case they are in the array).
>
> Warren Vail
>
> -----Original Message-----
> From: elias [mailto:elias_bachaalany@yahoo.com]
> Sent: Tuesday, July 31, 2001 4:05 AM
> To: php-general@lists.php.net
> Subject: [PHP] Re: storing array in mysql
>
> when you submit this form, PHP will give a array variable called $name
>
> you can store in in MySql as:
>
> <?
> // will make the $name as a comma seperated string
> $str = join(",", $name);
> insert into tablename(id, value) VALUES(null, '$str');
> ?>
>
> now to reget the array, you can select it back from MySql and split it as:
> <?
> $name = split(",", $str);
> ?>
>
> //elias
> "Matthew Delmarter" <matt@adplusonline.com> wrote in message
> news:NDBBKBIHLCDMDICHLDKGIECOCGAA.matt@adplusonline.com...
> > Hi all,
> >
> > I want to store the results of a multiple select input box in a mysql
> > db. The box looks like this:
> > <select name='name[]' size='5' multiple>
> > <option value='id'>name</option>
> > </select>
> >
> > I cannot seem to store the array in a database and then output the
> > result using foreach. Any tips?
> >
> > Regards,
> >
> > Matthew Delmarter
> > Web Developer
> >
> > AdplusOnline.com Ltd
> > www.adplusonline.com
> >
> > Phone: 06 8357684
> > Cell: 025 2303630
> >
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net