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

Sessions and mySql

P: n/a
Ken
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("INSERT INTO master (id) Values('$_SESSION['id']' ) " ) ;

Ken
Jul 17 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Ken (kk******@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("INSERT INTO master (id) Values('$_SESSION['id']' ) " ) ;

(untested code below)

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

$session_id = $_SESSION['id'];
mysql_query("INSERT 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.
Jul 17 '05 #2

P: n/a
Ken
"Malcolm Dew-Jones" <yf***@vtn1.victoria.tc.ca> wrote in message
news:42******@news.victoria.tc.ca...
Ken (kk******@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("INSERT INTO master (id) Values('$_SESSION['id']' ) " ) ;

(untested code below)

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

$session_id = $_SESSION['id'];
mysql_query("INSERT 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.
Ken
Jul 17 '05 #3

P: n/a
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.
http://php.net/types.string

Jul 17 '05 #4

P: n/a

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("INSERT INTO master (id) Values('$localid' ) " ) ;

Alternatively I think you can use the php sprintf command:

mysql_query("INSERT INTO master (id) Values('" . sprintf("%s",
$_SESSION['id']) . "' ) " ) ;
-----Original Message-----
From: Ken [mailto:kk******@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("INSERT INTO master (id) Values('$_SESSION['id']' ) " ) ;

Ken
Jul 17 '05 #5

P: n/a
Ken (kk******@wi.rr.com) wrote:
: "Malcolm Dew-Jones" <yf***@vtn1.victoria.tc.ca> wrote in message
: news:42******@news.victoria.tc.ca...
: > Ken (kk******@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("INSERT INTO master (id) Values('$_SESSION['id']' ) " ) ;
: >
: > (untested code below)
: >
: > The simplest change I can think of is something like the following
: >
: > $session_id = $_SESSION['id'];
: > mysql_query("INSERT 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}$_SESSION['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.
Jul 17 '05 #6

P: n/a
Ken wrote:
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("INSERT INTO master (id) Values('$_SESSION['id']' ) " ) ;

Ken


INSERT
INTO master (id)
VALUES ('{$_SESSION['id']}')
Jul 17 '05 #7

P: n/a
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.

Jul 17 '05 #8

P: n/a
Ken wrote:
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?


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
Jul 17 '05 #9

P: n/a
On Mon, 25 Apr 2005 22:35:53 +0000, Ken wrote:
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("INSERT INTO master (id) Values('$_SESSION['id']' ) " ) ;

Ken

mysql_query("INSERT INTO master (id) Values('" . $_SESSION['id'] . "')" );

or

mysql_query("INSERT INTO master (id) Values(\"$_SESSION['id']\")" ) ;

(approximately) spring to mind.

Steve
Jul 17 '05 #10

P: n/a
Good idea. Something like:

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

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

Jul 17 '05 #11

P: n/a
Steve, the second example will result in a parse error.

Jul 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.