note 48848 deleted from function.session-register by tularis
| From: | tularis@php.net | Date: | Sun, 09 Jan 2005 09:54:49 +0000 |
| Subject: | note 48848 deleted from function.session-register by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-82978@lists.php.net to get a copy of this message | ||
Note Submitter: mattmc8 at msn dot com
Reason: useless note
----
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.