Sessions and mySql

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

    Sessions and mySql

    This does not work because of the need for 's.

    Is there a way to insert a $_SESSION['id'] variable into a database?

    Thanks.

    mysql_query("IN SERT INTO master (id) Values('$_SESSI ON['id']' ) " ) ;

    Ken


  • Malcolm Dew-Jones

    #2
    Re: Sessions and mySql

    Ken (kkrolski@wi.rr .com) wrote:
    : This does not work because of the need for 's.

    : Is there a way to insert a $_SESSION['id'] variable into a database?

    There are many ways.


    : mysql_query("IN SERT INTO master (id) Values('$_SESSI ON['id']' ) " ) ;

    (untested code below)

    The simplist change I can think of is something like the following

    $session_id = $_SESSION['id'];
    mysql_query("IN SERT INTO master (id) Values( '$session_id' ) " ) ;


    You should also read up on bind variables, which might be even simpler but
    I haven't used them enough in php to write an example off the top of my
    head.




    --

    This space not for rent.

    Comment

    • Ken

      #3
      Re: Sessions and mySql

      "Malcolm Dew-Jones" <yf110@vtn1.vic toria.tc.ca> wrote in message
      news:426d7785@n ews.victoria.tc .ca...[color=blue]
      > Ken (kkrolski@wi.rr .com) wrote:
      > : This does not work because of the need for 's.
      >
      > : Is there a way to insert a $_SESSION['id'] variable into a database?
      >
      > There are many ways.
      >
      > : mysql_query("IN SERT INTO master (id) Values('$_SESSI ON['id']' ) " ) ;
      >
      > (untested code below)
      >
      > The simplest change I can think of is something like the following
      >
      > $session_id = $_SESSION['id'];
      > mysql_query("IN SERT INTO master (id) Values( '$session_id' ) " ) ;
      >
      >
      > You should also read up on bind variables, which might be even simpler but
      > I haven't used them enough in php to write an example off the top of my
      > head.[/color]

      I have 70 variables to insert into the database; all SESSION variables. I
      would rather stay with the session variables if possible.

      Is there a way to use the $_SESSION variable syntax?
      What are bind variables. They are not mentioned in my PHP book.
      Ken


      Comment

      • Philip  Olson

        #4
        Re: Sessions and mySql

        Sessions aren't special variables, $_SESSION is just an array like any
        other. There are several syntaxes for using arrays within strings
        (something you're trying to do above) for example:

        $str = "I am a string with an {$array['inside']} of me";
        $str = "I am also a string with an $array[inside] of me";
        $str = "I am another example with an " . $array['inside'] . " of me";

        See also the manual section on arrays and strings.


        Comment

        • Eugen Walcher

          #5
          Re: Sessions and mySql


          I had the same thing on my site.

          I had to post the session variable to a local one because php
          complained about whitespace characters

          Change your code to:

          $localid = $_SESSION['id'];
          mysql_query("IN SERT INTO master (id) Values('$locali d' ) " ) ;

          Alternatively I think you can use the php sprintf command:

          mysql_query("IN SERT INTO master (id) Values('" . sprintf("%s",
          $_SESSION['id']) . "' ) " ) ;


          -----Original Message-----
          From: Ken [mailto:kkrolski @wi.rr.com]
          Posted At: Tuesday, 26 April 2005 6:36 AM
          Posted To: comp.lang.php
          Conversation: Sessions and mySql
          Subject: Sessions and mySql

          This does not work because of the need for 's.

          Is there a way to insert a $_SESSION['id'] variable into a database?

          Thanks.

          mysql_query("IN SERT INTO master (id) Values('$_SESSI ON['id']' ) " ) ;

          Ken


          Comment

          • Malcolm Dew-Jones

            #6
            Re: Sessions and mySql

            Ken (kkrolski@wi.rr .com) wrote:
            : "Malcolm Dew-Jones" <yf110@vtn1.vic toria.tc.ca> wrote in message
            : news:426d7785@n ews.victoria.tc .ca...
            : > Ken (kkrolski@wi.rr .com) wrote:
            : > : This does not work because of the need for 's.
            : >
            : > : Is there a way to insert a $_SESSION['id'] variable into a database?
            : >
            : > There are many ways.
            : >
            : > : mysql_query("IN SERT INTO master (id) Values('$_SESSI ON['id']' ) " ) ;
            : >
            : > (untested code below)
            : >
            : > The simplest change I can think of is something like the following
            : >
            : > $session_id = $_SESSION['id'];
            : > mysql_query("IN SERT INTO master (id) Values( '$session_id' ) " ) ;
            : >
            : >
            : > You should also read up on bind variables, which might be even simpler but
            : > I haven't used them enough in php to write an example off the top of my
            : > head.

            : I have 70 variables to insert into the database; all SESSION variables. I
            : would rather stay with the session variables if possible.

            : Is there a way to use the $_SESSION variable syntax?
            : What are bind variables. They are not mentioned in my PHP book.


            Another easy to describe solution might be to use a variable for the quote
            character


            $quote="'";

            "INSERT INTO master (id) Values( $quote$_SESSION['id']$quote)"

            You might need to write that as ${quote}

            "INSERT INTO master (id) Values( ${quote}$_SESSI ON['id']${quote})"


            Others have given other example techniques.


            Bind variables, google can describe them I'm sure.


            You write sql that looks sort of like this

            "INSERT INTO master (id) Values(?)"


            The ? is where the bind variable is happening. You assign a value for the
            bind variable by using a seperate function call. Using them may look more
            complicated at first but they are often simpler in the long run. I can't
            write php for this sort of stuff without looking it up in the manual, so I
            won't give examples.



            --

            This space not for rent.

            Comment

            • Marcus

              #7
              Re: Sessions and mySql

              Ken wrote:[color=blue]
              > This does not work because of the need for 's.
              >
              > Is there a way to insert a $_SESSION['id'] variable into a database?
              >
              > Thanks.
              >
              > mysql_query("IN SERT INTO master (id) Values('$_SESSI ON['id']' ) " ) ;
              >
              > Ken
              >
              >[/color]

              INSERT
              INTO master (id)
              VALUES ('{$_SESSION['id']}')

              Comment

              • Philip  Olson

                #8
                Re: Sessions and mySql

                There is no need for $quote, that's a little crazy :) The problem is
                that the following will give a fatal parse error:

                $string = "I am $wrong['so'] so do not do this";

                It's only wrong because the ' is used for the array key. See my earlier
                examples for how to properly do that. Regarding this $quote thing, one
                can do the following:

                $string = "This isn't a problem at all";
                $string = 'This is "not" a problem either';
                $string = "This is \"another\" way to do it";
                $string = 'And \'this\' is yet another';

                Note the mixture of ' and ", this is important. The PHP manual is very
                clear on all of this.

                Comment

                • Marcin Dobrucki

                  #9
                  Re: Sessions and mySql

                  Ken wrote:
                  [color=blue]
                  > I have 70 variables to insert into the database; all SESSION variables. I
                  > would rather stay with the session variables if possible.
                  >
                  > Is there a way to use the $_SESSION variable syntax?[/color]

                  I can think of two ways to try to solve this:

                  1) implode/explode functions
                  2) A bit of fiddling with DB_DataObject from PEAR might help.
                  Alternatively, move the whole thing under HTTP_Session or HTTP_Session2,
                  although then one has to deal with beta code

                  /M

                  Comment

                  • Steve

                    #10
                    Re: Sessions and mySql

                    On Mon, 25 Apr 2005 22:35:53 +0000, Ken wrote:
                    [color=blue]
                    > This does not work because of the need for 's.
                    >
                    > Is there a way to insert a $_SESSION['id'] variable into a database?
                    >
                    > Thanks.
                    >
                    > mysql_query("IN SERT INTO master (id) Values('$_SESSI ON['id']' ) " ) ;
                    >
                    > Ken[/color]
                    mysql_query("IN SERT INTO master (id) Values('" . $_SESSION['id'] . "')" );

                    or

                    mysql_query("IN SERT INTO master (id) Values(\"$_SESS ION['id']\")" ) ;

                    (approximately) spring to mind.

                    Steve

                    Comment

                    • Philip  Olson

                      #11
                      Re: Sessions and mySql

                      Good idea. Something like:

                      $sql = "INSERT INTO sometable VALUES ('" . implode("','", $somearray) .
                      "')";

                      To do this you really have to know your data though...

                      Comment

                      • Philip  Olson

                        #12
                        Re: Sessions and mySql

                        Steve, the second example will result in a parse error.

                        Comment

                        Working...