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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim S.

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

    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

    #2
    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]

    Generates a storable representation of a value



    JW


    Comment

    • Sean Barton

      #3
      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

      Comment

      • Janwillem Borleffs

        #4
        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


        Comment

        • Jim S.

          #5
          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.c om> wrote in message
          news:43e5eb17$0 $40776$dbd4d001 @news.euronet.n l...[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]


          Comment

          • xclarky@gmail.com

            #6
            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.

            Comment

            • xclarky@gmail.com

              #7
              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.

              Comment

              • Jim S.

                #8
                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.

                helllllllllllll p :D
                <xclarky@gmail. com> wrote in message
                news:1139144662 .354557.102930@ f14g2000cwb.goo glegroups.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]


                Comment

                • David Haynes

                  #9
                  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.
                  >
                  > helllllllllllll p :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.descripti on
                  from codes, options, main
                  where codes.code_numb er = options.code_nu mber
                  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_nu mber
                  from options, main
                  where options.unique_ id = main.unique_id
                  and main.unique_id = $unique_id
                  order by options.code_nu mber;

                  for a sorted list.

                  -david-

                  Comment

                  • Jim Michaels

                    #10
                    Re: an array to a database, am having hard time , help!!

                    "Jim S." <jim@yeah.com > wrote in message
                    news:d3oFf.1091 6$rH5.4077@news read2.news.atl. earthlink.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(colum nname) 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.
                    >
                    > helllllllllllll p :D
                    > <xclarky@gmail. com> wrote in message
                    > news:1139144662 .354557.102930@ f14g2000cwb.goo glegroups.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]


                    Comment

                    • Jim Michaels

                      #11
                      Re: an array to a database, am having hard time , help!!


                      "Jim Michaels" <jmichae3@nospa m.yahoo.com> wrote in message
                      news:xr-dncOKpOqAoXXeRV n-oQ@comcast.com. ..[color=blue]
                      > "Jim S." <jim@yeah.com > wrote in message
                      > news:d3oFf.1091 6$rH5.4077@news read2.news.atl. earthlink.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(colum nname) 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_esca pe_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.
                      >>
                      >> helllllllllllll p :D
                      >> <xclarky@gmail. com> wrote in message
                      >> news:1139144662 .354557.102930@ f14g2000cwb.goo glegroups.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]


                      Comment

                      • Jim S.

                        #12
                        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@nospa m.yahoo.com> wrote in message
                        news:3eidnUtKWY 1303XeRVn-uA@comcast.com. ..[color=blue]
                        >
                        > "Jim Michaels" <jmichae3@nospa m.yahoo.com> wrote in message
                        > news:xr-dncOKpOqAoXXeRV n-oQ@comcast.com. ..[color=green]
                        >> "Jim S." <jim@yeah.com > wrote in message
                        >> news:d3oFf.1091 6$rH5.4077@news read2.news.atl. earthlink.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(colum nname) 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_esca pe_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.
                        >>>
                        >>> helllllllllllll p :D
                        >>> <xclarky@gmail. com> wrote in message
                        >>> news:1139144662 .354557.102930@ f14g2000cwb.goo glegroups.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]


                        Comment

                        Working...