Nested FOREACH loops are very expensive in terms of processor usage compared to sessions. Just use sessions -- it's what they're there for. However, they are not secure as all sessions are doing is deciding if the browser allows cookies and if not, GET strings are appended to all links similar to what you've described (except you can do it with simple session_start(); line in PHP rather than a bunch of javascript code, but be sure to do this right at beginning of script before any output is sent to the browser. PHP sessions are also subject to same security issues cookies are, as I described in the other list this question was posted to. Hope this helps! Good luck!
Ken Krauss http://www.kcwebdev.com --- In [email protected], <[EMAIL PROTECTED]> wrote: > > ----- Original Message ----- > From: "David Halliday" <[EMAIL PROTECTED]> > To: "David Halliday" <[EMAIL PROTECTED]> > Sent: Saturday, June 23, 2007 2:27 PM > Subject: [php-list] Sessions or MYSQL .. Which is better? > > > Hello > > I have this html page which is loaded after a user > logs in. The variables on that page are populated from > one or more rows of data which of course belongs to > the user in question. The user may click a link on > the page to change a password or email and when the > operation is completed the page reloads again. > > Now the way I have been doing this is to pull an array > of data from the database *every time* the user loads > the page. I have recently read somewhere on the net > that "most of the time is spent in the actual > connection with the database .. the remaining > operations are relatively insignificant, time-wise." > > Would it be safe on memory and overall performance if > I stored the array as one session variable instead of > several connections with the database? > > Would that be a good idea even if the session variable > is large? (several rows of values - about 50). > > Would greatly appreciate an expert advice. > > Regards, > > David > ----------------------------- > Hi David, > Your will be using memory regardless of weather you are using > it as PHP variables within a script or as PHP variables registered into a > session so it is more an issue of how long you are storing the variables for > and how many concurrent threads you have. > > If you are going to register variables into a session and keep that same > session and variables sitewide then you are going to have a lot of > concurrent treads that are keeping these variables. It may well be the case > that you use these variables on many pages anyway so registering them into > the session may be no big deal. > > Generally if I have non-sensitive information that as to be passed between a > small number of pages then I append them as GET or POST methods into the > pages them selves. ie - > <input type="hidden" name="data" value="100" > <input type="hidden" name="userid" value="123" > OR > <a href="nextpage.php?data=100&userid=123" > > You can get fancy and use unset($_SESSION['xxx']) or session_destroy() but > then you have to include an extra script at the end of every page of the > site - > > ------------------ > some_file.php > <?php > // > $keep_user_data = TRUE; > include("session_keeper.php"); > ?> > ------------------ > some_other_file.php > <?php > // > include("session_keeper.php"); > ?> > ------------------ > session_keeper.php > <?php > if($keep_user_data != TRUE){ > unset($_SESSION["user_data"]); > } > ?> > ------------------ > There are alternatives to registering an array that will likely use less > memory - > ------------------ > The normal way - > <?php > query$ = "SELECT ... > $handle = mysql_connect(... > $result = mysql_query($handle, $query ... > foreach($result as $row){ > foreach($row as $key => $value){ > myarray[][$key] = $value; > } > } > session_register($myarray); > ... > ------------------- > A sort cut - > <?php > if (!isset($_SESSION["result"])){ > query$ = "SELECT ... > $handle = mysql_connect(... > $result = mysql_query($handle, $query ... > session_register($result); > } > > foreach($_SESSION["result"] as $row){ > foreach($row as $key => $value){ > userdata[][$key] = $value; > } > } > --------------------- > This leads me to another question that I hope someone else can answer ??? > > Can you simply register the mySQL connection into the session ?? or will the > connection be closed when a single page completes ??? > > --------------------- > ??????? > <?php > if (!isset($_SESSION("handle))){ > $handle = mysql_connect(... > session_register($handle); > } > > $query="... > mysql_query($handle, $query ... > > Thanks, Rob. >
