472,785 Members | 1,224 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,785 software developers and data experts.

Sessions and mySql

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
11 2002
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
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
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

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
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
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
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
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
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
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
Steve, the second example will result in a parse error.

Jul 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Chewy509 | last post by:
Hi Everyone, I'll just start, and say I am not a PHP developer (I'm a sysadmin, who has gotten lumped with a non-working website). But since I like to do this type of stuff, I though I might...
7
by: John | last post by:
Hello. I want to get this blasted .htaccess file sorted out, so I can have sessions without register_globals being on. I have looked everywhere for info on this and I mean everywhere...
5
by: bryan | last post by:
Hi folks, I've got a question about sessions in PHP. I'm a *gulp* ASP coder, so please bear with me. I developed a PHP site a few months back that has a password protected entry into a...
1
by: windandwaves | last post by:
Hi Gurus I am basically sorry that I have to bother you about this. I am a PHP beginner and I have been studying sessions and cookies over the last few weeks. I have learned lots, but I am...
13
by: jamie howard | last post by:
Hello there - we have a fairly busy server and we just started to have problems with PHP sessions failing. We've never had this problem before and to be honist, out server traffic is lower than it...
4
by: ctclibby | last post by:
Hi All Seem to be getting zombie sessions. /tmp/sess_ exist and are owned by daemon. I am guessing and these could come from brower crashes, networks gone down ... etc ... even from stuff that...
6
by: bill | last post by:
I have been "Googling" for about an hour and am turning up squat! I just started receiving this error when trying to log into a MS Access database from a vb .net web application. Recycling IIS...
9
by: Gilles Ganault | last post by:
Hello Some data are common to all user sessions, and to improve performance/save resources, I'd like to share those data between all sessions, so that each user doesn't have to hit MySQL for the...
1
by: rajesh0303 | last post by:
Hi. Problem is that all sessions are removed when I am using this code... By this code I am deleting files in a particular folder and then that folder.. Need help...... code is: try
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.