basic sessions help pls!!

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
gpittingale
Forum Newbie
Posts: 12
Joined: Fri Jan 23, 2009 5:58 am

basic sessions help pls!!

Post by gpittingale »

Hi everyone I have two pages i would like to pass data between however the data i would like to pass is acually data that had been called from a mysql and automatically put into arrays as below

Code: Select all

$sql_query = "SELECT * FROM base_data WHERE ParishName LIKE '$chooser'";
$result = mysql_query($sql_query,$conn) or die('Invalid query: ' . mysql_error());
$details = mysql_fetch_array($result);
Now i would like to use the same set of arrays in the next page, according to what was selected in the form How do i go about doing this I know i have to put a

Code: Select all

session_start();
at the start of eache page then call them as

Code: Select all

$_SESSION['myarray']
but the only tutorials i can find tell you to define the arrays manualy which could be a long drawn out process.
Could someone please give me an example to work with so i can sort this out onc and for all
thanks in advance.
User avatar
nor0101
Forum Commoner
Posts: 53
Joined: Thu Jan 15, 2009 12:06 pm
Location: Wisconsin

Re: basic sessions help pls!!

Post by nor0101 »

gpittingale wrote:Hi everyone I have two pages i would like to pass data between however the data i would like to pass is acually data that had been called from a mysql and automatically put into arrays as below

Code: Select all

$sql_query = "SELECT * FROM base_data WHERE ParishName LIKE '$chooser'";
$result = mysql_query($sql_query,$conn) or die('Invalid query: ' . mysql_error());
$details = mysql_fetch_array($result);
Now i would like to use the same set of arrays in the next page, according to what was selected in the form How do i go about doing this I know i have to put a

Code: Select all

session_start();
at the start of eache page then call them as

Code: Select all

$_SESSION['myarray']
but the only tutorials i can find tell you to define the arrays manualy which could be a long drawn out process.
Could someone please give me an example to work with so i can sort this out onc and for all
thanks in advance.
Well, $details is already an array. You can store it in the current session by doing

Code: Select all

 
$session_name('myname');
$session_start();
 
// code to fetch info from db goes here
 
$_SESSION['details'] = $details;
 
You can access the information on subsequent pages like so:

Code: Select all

 
session_name('myname');
$session_start();
 
$details = $_SESSION['details'];
 
// at this point the array $details is available as if you had just fetched it.
...
 
gpittingale
Forum Newbie
Posts: 12
Joined: Fri Jan 23, 2009 5:58 am

Re: basic sessions help pls!!

Post by gpittingale »

just tryed what you said and for some reason it does not want to work? so hopefully youl get what i mean from this.....
here is the code for the head in the first page as you described

Code: Select all

<?php 
session_name('scdc');
session_start();
$HOST = 'localhost';
$DATABASE = '****';
$USER = '*****';
$PASSWORD = '*****';
// connect to database
if(!$conn=mysql_connect('localhost','*******','*****')) {
echo("<li>Can't connect to $HOST as $USER");
echo("<li>mysql Error: ".mysql_error());
die;
}
 
// select database
if (!mysql_select_db($DATABASE,$conn)) {
echo("<li>We were unable to select database $DATABASE");
die;
}
[color=#FF4000]$sql_query = "SELECT * FROM base_data WHERE ParishName LIKE '$chooser'";
$result = mysql_query($sql_query,$conn) or die('Invalid query: ' . mysql_error());
$details = mysql_fetch_array($result);
$_SESSION['details'] = $details;[/color]
?>
The peice in red allows me to call the arrays as

Code: Select all

$details['ParishName'];
so on the second page i now have

Code: Select all

<?php 
session_name('scdc');
session_start();
$details = $_SESSION['details'];
?>
and i then called an array like i normally do

Code: Select all

&details['ParishName']
should this be correct or am i stupid
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: basic sessions help pls!!

Post by califdon »

If you're going to address the array as a key->value pair, use mysql_fetch_assoc() instead of mysql_fetch_array().
Post Reply