note 48848 added to function.session-register

From: Date: Sun, 09 Jan 2005 06:55:12 +0000
Subject: note 48848 added to function.session-register
Groups: php.notes 
Request: Send a blank email to php-notes+get-82972@lists.php.net to get a copy of this message
This is a very useful way to produce dynamic content for a site with sessions and mysql and arrays. A lot of people should like this as I have found it very, very useful for making fewer calls to the db for sites based on user settings or content management that may have a lot of user settings in a db and want to make them into sessions var's with few queries to keep the db connects down from page to page. $connect = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname); $query = mysql_query("SELECT My_Column FROM My_Table"); $i = 0; while($result = mysql_fetch_assoc($query)){ while(list($k, $v)= each($result)){ $_SESSION[$k][$i] = $v; $i++; } } Produces: Ex. $_SESSION['My_Column'][0] = data on row one in column $_SESSION['My_Column'][1] = data on row two in MyColumn What this does is it makes arrays of sessions variables with the data gathered from a column in a db, so it's good for catagorizing variables from a database to be used throughout the remainder of the session without another db call. Be careful when calling more than one column at a time as it will loop through row one assigning $i to each column data returned before starting row2. Use print_r($_SESSION) to see your session array to see what I'm saying. ---- Manual Page -- http://www.php.net/manual/en/function.session-register.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+48848 Delete -- http://master.php.net/manage/user-notes.php?action=delete+48848&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48848&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48848&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48848&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48848&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48848&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+48848&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#82972) next »