By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,570 Members | 1,355 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,570 IT Pros & Developers. It's quick & easy.

an array to a database, am having hard time , help!!

P: n/a
hi guys and gals,
i have an array, but i have the hardest time putting the value in the table,
so what is the normal procedure?

thanks
Jim
Feb 5 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Jim S. wrote:
hi guys and gals,
i have an array, but i have the hardest time putting the value in the
table, so what is the normal procedure?


http://www.php.net/serialize
JW
Feb 5 '06 #2

P: n/a
i dont think serialization is the answer to this one. maybe its just a
simple do while and sql insert?

Sean Barton

Feb 5 '06 #3

P: n/a
Sean Barton wrote:
i dont think serialization is the answer to this one. maybe its just a
simple do while and sql insert?


Without a proper description, I'm assuming that the OP wants to store an
array into a single db field.

JW
Feb 5 '06 #4

P: n/a
yes, exactly, i want to insert / update a database record with an array.
i tried "implode" and i got a variable that has the values seperated with a
comma, but i could never succeed to insert that value in the database.

the variable after the implode was as following: $mystuff and has the
values: 1,3,5,4,6,7

but i am not able to insert them. so what is the requirement of the field in
order to accept such a variable?
should i also use ENUM ??

thanks again

"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:43***********************@news.euronet.nl...
Sean Barton wrote:
i dont think serialization is the answer to this one. maybe its just a
simple do while and sql insert?


Without a proper description, I'm assuming that the OP wants to store an
array into a single db field.

JW

Feb 5 '06 #5

P: n/a
ENUM is basically the same as SET however it allows NULL values.
Therefore this would be a wrong data type to use considering what you
are storing.

As you are storing an array, I would advise you to break it town to
text so you basically have a list of numbers, as you have done.

You can then store that list in your field, VARCHAR or TEXT should be
appropriate depending on the length of the list.

When retreiving the data you can use explode() or array_map() to create
an array of the numbers for whatever purpose you wish.

Hope that helps.

Feb 5 '06 #6

P: n/a
ENUM is basically the same as SET however it allows NULL values.
Therefore this would be a wrong data type to use considering what you
are storing. Both basically set up a list of acceptable values for the
field.

As you are storing an array, I would advise you to convert it to a
string so you have a list of numbers, separated by a divider; as you
have done.

You can then store that list in your field, VARCHAR or TEXT should be
appropriate depending on the length of the list.

When retrieving the data you can use explode() or array_map() to create
an array of the numbers for whatever purpose you wish. Alternatively
you can use MySQL functions in the WHERE clause for statements if you
are looking to perform a action where a match is found for a specific
value in your list.

Hope that helps you.

Feb 5 '06 #7

P: n/a
well i got lastly, a variable that is like: $mystuff and has 1,4,3,6,7,8

but i am not able to put $mystuff in the table no matter what i do.
my table field is of a type Varchar(255) .

now let us assume that i succeed (am being Very optimistic, emphasic on
VERY)
i know i can use explode, but i want to change the 1,4,3,6,7,8 to the
option that i started with. so how can i do that?
so i can have instead of numbers, basically, to have option1, option4,
option3, ... and so on.

helllllllllllllp :D
<xc*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
ENUM is basically the same as SET however it allows NULL values.
Therefore this would be a wrong data type to use considering what you
are storing. Both basically set up a list of acceptable values for the
field.

As you are storing an array, I would advise you to convert it to a
string so you have a list of numbers, separated by a divider; as you
have done.

You can then store that list in your field, VARCHAR or TEXT should be
appropriate depending on the length of the list.

When retrieving the data you can use explode() or array_map() to create
an array of the numbers for whatever purpose you wish. Alternatively
you can use MySQL functions in the WHERE clause for statements if you
are looking to perform a action where a match is found for a specific
value in your list.

Hope that helps you.

Feb 5 '06 #8

P: n/a
Jim S. wrote:
well i got lastly, a variable that is like: $mystuff and has 1,4,3,6,7,8

but i am not able to put $mystuff in the table no matter what i do.
my table field is of a type Varchar(255) .

now let us assume that i succeed (am being Very optimistic, emphasic on
VERY)
i know i can use explode, but i want to change the 1,4,3,6,7,8 to the
option that i started with. so how can i do that?
so i can have instead of numbers, basically, to have option1, option4,
option3, ... and so on.

helllllllllllllp :D


Jim,
I really wonder why you are going to all this trouble to code stuff that
is so easily modeled in the database. Is there some really compelling
reason not to use the power of the database to do your work for you?

Consider the following:
1. a table that contains a code number and a text field (lets call it codes)
2. a table that holds the non-dynamic stuff around your array. At a
minimum it has a unique id column and a description column. (lets call
it main)
3. a table that contains the unique id from the main table and a code
number from the codes table for each option in the array (lets call it
options)

Now, you can do things like:
select codes.description
from codes, options, main
where codes.code_number = options.code_number
and options.unique_id = main.unique_id
and main.unique_id = $unique_id;

to get all the selection options (as text) for an entry in your main table.

If you just wanted the code numbers, you could use:
select options.code_number
from options, main
where options.unique_id = main.unique_id
and main.unique_id = $unique_id
order by options.code_number;

for a sorted list.

-david-

Feb 5 '06 #9

P: n/a
"Jim S." <ji*@yeah.com> wrote in message
news:d3******************@newsread2.news.atl.earth link.net...
well i got lastly, a variable that is like: $mystuff and has
1,4,3,6,7,8

but i am not able to put $mystuff in the table no matter what i do.
have you tried putting single quotes around the data when you INSERT it?
INSERT INTO tablename(columnname) VALUES ('1,4,3,6,7,8')

my table field is of a type Varchar(255) .

now let us assume that i succeed (am being Very optimistic, emphasic on
VERY)
i know i can use explode, but i want to change the 1,4,3,6,7,8 to the
option that i started with. so how can i do that?
so i can have instead of numbers, basically, to have option1, option4,
option3, ... and so on.

helllllllllllllp :D
<xc*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
ENUM is basically the same as SET however it allows NULL values.
Therefore this would be a wrong data type to use considering what you
are storing. Both basically set up a list of acceptable values for the
field.

As you are storing an array, I would advise you to convert it to a
string so you have a list of numbers, separated by a divider; as you
have done.

You can then store that list in your field, VARCHAR or TEXT should be
appropriate depending on the length of the list.

When retrieving the data you can use explode() or array_map() to create
an array of the numbers for whatever purpose you wish. Alternatively
you can use MySQL functions in the WHERE clause for statements if you
are looking to perform a action where a match is found for a specific
value in your list.

Hope that helps you.


Feb 7 '06 #10

P: n/a

"Jim Michaels" <jm******@nospam.yahoo.com> wrote in message
news:xr********************@comcast.com...
"Jim S." <ji*@yeah.com> wrote in message
news:d3******************@newsread2.news.atl.earth link.net...
well i got lastly, a variable that is like: $mystuff and has
1,4,3,6,7,8

but i am not able to put $mystuff in the table no matter what i do.


or more specifically,

have you tried putting single quotes around the data when you INSERT it?
$q=mysql_query("INSERT INTO tablename(columnname) VALUES ('$mystuff')",
$link);
if you are having problems with the length of your data, change the type
from VARCHAR to LONGTEXT.

if you want a more direct method of storing arrays because you have strings
in them or quotes, commas, or other SQL punctuation and such, then use
serialize(), unserialize() and a TINYBLOB, BLOB(), or MEDIUMBLOB data type.
with binary data types like that, if you have PHP5 and MYSQL5, try using the
mysqli functions like mysqli_prepare(), etc. for your inserts. you'll get a
little more speed. you also don't have to encode your binary data with
mysql_real_escape_string().

my table field is of a type Varchar(255) .

now let us assume that i succeed (am being Very optimistic, emphasic on
VERY)
i know i can use explode, but i want to change the 1,4,3,6,7,8 to the
option that i started with. so how can i do that?
so i can have instead of numbers, basically, to have option1, option4,
option3, ... and so on.

helllllllllllllp :D
<xc*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
ENUM is basically the same as SET however it allows NULL values.
Therefore this would be a wrong data type to use considering what you
are storing. Both basically set up a list of acceptable values for the
field.

As you are storing an array, I would advise you to convert it to a
string so you have a list of numbers, separated by a divider; as you
have done.

You can then store that list in your field, VARCHAR or TEXT should be
appropriate depending on the length of the list.

When retrieving the data you can use explode() or array_map() to create
an array of the numbers for whatever purpose you wish. Alternatively
you can use MySQL functions in the WHERE clause for statements if you
are looking to perform a action where a match is found for a specific
value in your list.

Hope that helps you.



Feb 7 '06 #11

P: n/a
hey jim, thanks , i will try that, thanks a lot. and that new mysqli_prepare
is interesting, i will check it out.

i finally solved the problem in a funcky way, (using serialize though)
instead of saving the values (1, 2,3,,,...) i saved the labels ofthe menus,
basically, i had a label same as the value, and did the
serialize/unserialize

since i thought, it will be a hassle for me after saving the numerical
values to get the "labels" associated with it. (any comment on that?)
further more , i used a multi select menu, like it was suggested to me
instead the file select/menus and when echoing it i just did a
while/endwhile .

that's it folks, i really thank everyone that pitched in for the help.
thanks
Jim

"Jim Michaels" <jm******@nospam.yahoo.com> wrote in message
news:3e********************@comcast.com...

"Jim Michaels" <jm******@nospam.yahoo.com> wrote in message
news:xr********************@comcast.com...
"Jim S." <ji*@yeah.com> wrote in message
news:d3******************@newsread2.news.atl.earth link.net...
well i got lastly, a variable that is like: $mystuff and has
1,4,3,6,7,8

but i am not able to put $mystuff in the table no matter what i do.


or more specifically,

have you tried putting single quotes around the data when you INSERT it?
$q=mysql_query("INSERT INTO tablename(columnname) VALUES ('$mystuff')",
$link);
if you are having problems with the length of your data, change the type
from VARCHAR to LONGTEXT.

if you want a more direct method of storing arrays because you have
strings in them or quotes, commas, or other SQL punctuation and such, then
use serialize(), unserialize() and a TINYBLOB, BLOB(), or MEDIUMBLOB data
type.
with binary data types like that, if you have PHP5 and MYSQL5, try using
the mysqli functions like mysqli_prepare(), etc. for your inserts. you'll
get a little more speed. you also don't have to encode your binary data
with mysql_real_escape_string().

my table field is of a type Varchar(255) .

now let us assume that i succeed (am being Very optimistic, emphasic on
VERY)
i know i can use explode, but i want to change the 1,4,3,6,7,8 to the
option that i started with. so how can i do that?
so i can have instead of numbers, basically, to have option1, option4,
option3, ... and so on.

helllllllllllllp :D
<xc*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
ENUM is basically the same as SET however it allows NULL values.
Therefore this would be a wrong data type to use considering what you
are storing. Both basically set up a list of acceptable values for the
field.

As you are storing an array, I would advise you to convert it to a
string so you have a list of numbers, separated by a divider; as you
have done.

You can then store that list in your field, VARCHAR or TEXT should be
appropriate depending on the length of the list.

When retrieving the data you can use explode() or array_map() to create
an array of the numbers for whatever purpose you wish. Alternatively
you can use MySQL functions in the WHERE clause for statements if you
are looking to perform a action where a match is found for a specific
value in your list.

Hope that helps you.



Feb 8 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.