I am trying to pass the values of an array from one PHP page to another but I am not having any luck. I need someone to tell me what I am doing wrong or if there is a bettter way.

Here is my code snippet for page one:

<?php

    //Page 1 - Get values from the database
    //This page is included from the main page

    session_start();

    $MYDB_CONNECTION = "something1";
    $MYDB_USER = "something2";
    $MYDB_PASS = "something3";

    $link = mysql_connect($MYDB_CONNECTION, $MYDB_USER, $MYDB_PASS) or die(mysql_error());

    if (!$link) { 
        die('Could not connect: ' . mysql_error() . "<br>" .  error_message); 
    } 
    // echo 'Connected successfully' . '<br>'; 
    mysql_select_db(customer); 

    $query = "SELECT * FROM board_members";
    $result = mysql_query($query) or die(mysql_error());    

    $data = array(); // create a variable to hold the information

    while (($row = mysql_fetch_array($result, MYSQL_ASSOC)) !== false)
    {
      $data[] = $row; // add the row in to the results (data) array
    }

    $_Session['serializedArray'] = serialize($data);
?>

Here is my code snippet for page 2:

<?
    //Page 2 - Test display of values from database

    session_start();

    $data = unserialize($_Session['serializedArray']);
    print_r ($data);
    print_r ("Third person is: ".$data[2]['FirstName']." ".$data[2]['LastName']."-".$data[2]['Amount']);

?> 

Recommended Answers

All 3 Replies

I'm not sure whether $_Session is the same as $_SESSION. I've been use the latter forever.

Also, you may want to output the $_Session['serializedArray'] in your first page. Just to ensure the data is really stored.

Totally agree with Javvy. $_SESSION will be more accurate.
And try to use print_r to print the array to make sure if the data is correct.
If these mmethod did not help, please provide us with the result or any errors you get.

The "test" pages I setup worked correctly but my production pages would not give an output.
After much "paying around" and "trouble shooting" I have been able to get it to work. I cannot say for sure, but it may have been not using the same declared session variable between pages or it may have been a small typo.

Thanks to all who responded.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.