473,786 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("IN SERT INTO master (id) Values('$_SESSI ON['id']' ) " ) ;

Ken
Jul 17 '05 #1
11 2050
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("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.
Jul 17 '05 #2
Ken
"Malcolm Dew-Jones" <yf***@vtn1.vic toria.tc.ca> wrote in message
news:42******@n ews.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("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.
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("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: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("IN SERT INTO master (id) Values('$_SESSI ON['id']' ) " ) ;

Ken
Jul 17 '05 #5
Ken (kk******@wi.rr .com) wrote:
: "Malcolm Dew-Jones" <yf***@vtn1.vic toria.tc.ca> wrote in message
: news:42******@n ews.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("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.
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("IN SERT INTO master (id) Values('$_SESSI ON['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("IN SERT INTO master (id) Values('$_SESSI ON['id']' ) " ) ;

Ken

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

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

Similar topics

6
7204
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 just learn WTF is going on? :) Basically, sessions are being created, but no info in being stored in the session, and if data is stored (about 1 in 20 goes), it doesn't follow-on on a page redirect.
7
7545
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 including the php.net manual. In the manual it said to include something like the following:
5
1741
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 secure section. I'm using session variables to ensure that track if a visitor has already logged in, and should therefore be allowed to view a given page.
1
2824
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 missing the big picture. Is it like this: 1. user comes to site 2. user does something (e.g. a search) that may be useful later => session
13
2356
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 has been in prior months - so I'm really not sure what could cause this. Could anyone suggest some potentiol avenues to explore for reasons that could cause PHP sessions to fail? Thanks for any suggestions!
4
3682
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 I haven't done properly. So for the big question. Can I run a cron job and delete these? Or does PHP also store stuff in another location and could cause me grief down the road? Thanks in advance!
6
3703
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 seems to temporarily fix the problem, but I am at a loss on how to view the total # of "active" sessions, etc... any ideas or suggestions would be greatly appreciated! Could not start session. Too many sessions already active. Description: An...
9
7383
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 same data. I'd rather avoid writing those in a flat file, and keep stuff in RAM instead. Someone told me about cache servers like MemCacheD. I was also given the hints of writing in OO (public class variables) or using
1
1085
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
9492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.