Connecting Tech Pros Worldwide Help | Site Map

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

Jim S.
Guest
 
Posts: n/a
#1: Feb 5 '06
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


Janwillem Borleffs
Guest
 
Posts: n/a
#2: Feb 5 '06

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


Jim S. wrote:[color=blue]
> 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?
>[/color]

http://www.php.net/serialize


JW


Sean Barton
Guest
 
Posts: n/a
#3: Feb 5 '06

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


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

Sean Barton

Janwillem Borleffs
Guest
 
Posts: n/a
#4: Feb 5 '06

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


Sean Barton wrote:[color=blue]
> i dont think serialization is the answer to this one. maybe its just a
> simple do while and sql insert?
>[/color]

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

JW


Jim S.
Guest
 
Posts: n/a
#5: Feb 5 '06

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


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:43e5eb17$0$40776$dbd4d001@news.euronet.nl...[color=blue]
> Sean Barton wrote:[color=green]
>> i dont think serialization is the answer to this one. maybe its just a
>> simple do while and sql insert?
>>[/color]
>
> Without a proper description, I'm assuming that the OP wants to store an
> array into a single db field.
>
> JW
>[/color]


xclarky@gmail.com
Guest
 
Posts: n/a
#6: Feb 5 '06

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


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.

xclarky@gmail.com
Guest
 
Posts: n/a
#7: Feb 5 '06

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


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.

Jim S.
Guest
 
Posts: n/a
#8: Feb 5 '06

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


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
<xclarky@gmail.com> wrote in message
news:1139144662.354557.102930@f14g2000cwb.googlegr oups.com...[color=blue]
> 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.
>[/color]


David Haynes
Guest
 
Posts: n/a
#9: Feb 5 '06

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


Jim S. wrote:[color=blue]
> 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[/color]

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-

Jim Michaels
Guest
 
Posts: n/a
#10: Feb 7 '06

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


"Jim S." <jim@yeah.com> wrote in message
news:d3oFf.10916$rH5.4077@newsread2.news.atl.earth link.net...[color=blue]
> 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.[/color]

have you tried putting single quotes around the data when you INSERT it?
INSERT INTO tablename(columnname) VALUES ('1,4,3,6,7,8')

[color=blue]
> 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
> <xclarky@gmail.com> wrote in message
> news:1139144662.354557.102930@f14g2000cwb.googlegr oups.com...[color=green]
>> 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.
>>[/color]
>
>[/color]


Jim Michaels
Guest
 
Posts: n/a
#11: Feb 7 '06

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



"Jim Michaels" <jmichae3@nospam.yahoo.com> wrote in message
news:xr-dncOKpOqAoXXeRVn-oQ@comcast.com...[color=blue]
> "Jim S." <jim@yeah.com> wrote in message
> news:d3oFf.10916$rH5.4077@newsread2.news.atl.earth link.net...[color=green]
>> 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.[/color]
>[/color]

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().
[color=blue]
>
>[color=green]
>> 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
>> <xclarky@gmail.com> wrote in message
>> news:1139144662.354557.102930@f14g2000cwb.googlegr oups.com...[color=darkred]
>>> 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.
>>>[/color]
>>
>>[/color]
>
>[/color]


Jim S.
Guest
 
Posts: n/a
#12: Feb 8 '06

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


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" <jmichae3@nospam.yahoo.com> wrote in message
news:3eidnUtKWY1303XeRVn-uA@comcast.com...[color=blue]
>
> "Jim Michaels" <jmichae3@nospam.yahoo.com> wrote in message
> news:xr-dncOKpOqAoXXeRVn-oQ@comcast.com...[color=green]
>> "Jim S." <jim@yeah.com> wrote in message
>> news:d3oFf.10916$rH5.4077@newsread2.news.atl.earth link.net...[color=darkred]
>>> 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.[/color]
>>[/color]
>
> 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().
>[color=green]
>>
>>[color=darkred]
>>> 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
>>> <xclarky@gmail.com> wrote in message
>>> news:1139144662.354557.102930@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.
>>>>
>>>
>>>[/color]
>>
>>[/color]
>
>[/color]


Closed Thread