Session Variables in Function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hbnmgr
Forum Newbie
Posts: 13
Joined: Mon Mar 07, 2005 8:22 pm

Session Variables in Function

Post by hbnmgr »

Did a Search but couldn't find an answer on the following ...

Problem:
In a Function, I want to get information, from the database, on a certain member and make these available as session variables. This way they can replace certain keywords on the page.

Q:
How do code the function to make the variables pulled as Session Variables?

In addition, can these be available on the first page, wherein the calls to the database, and the string replacements are on the same page? (because according to the manual, session variables are not available till the second page - utilizing session start)

Code so far ...

Code: Select all

function get_member_info()
{
// query database for member info
   $conn = db_connect();
   $query = "select memid, cbid, coname, firstname, lastname, address, city, state, zip, phone, mememail, otherlink, opfield1, opfield2
             from members where memdir = $memdir";
   $result = @$conn->query($query);
   if (!$result)
   	return false;
   
}
Thanks!
SRR


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

im not real sure what you are asking but i think you want the results of the query to be made into session variables. do it like this

Code: Select all

$query = "select memid, cbid, coname, firstname, lastname, address, city, state, zip, phone, mememail, otherlink, opfield1, opfield2
             from members where memdir = $memdir";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)){
$_SESSION[memid] = $row[memid];
$_SESSION[cbid] = $row[cbid];
$_SESSION[coname] = $row[coname];
$_SESSION[firstname] = $row[firstname];
//finish all the things you want
}
but make sure you have session_start(); at the top of your page.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

remember to quote your named array indicies, or string literals..
Post Reply