0

I make two php pages one contain all the values of the row in some variables and the other page to call that variables like below.

function students_table($students_table_row) 
{
$select_db = "select * from students where p_username ='".$students_table_row."'";
$student_address_query= mysql_query($select_db);
$students_db = mysql_fetch_assoc($student_address_query);

$student_id_db = $students_db['student_id'];
$student_pet_name_db = $students_db['student_pet_name']; 
$student_name_db = $students_db['student_name'];
$p_gender_db = $students_db['p_gender'];
}

On the other page I want to insert the value for $students_table_row and than I want to read any variables like $student_id_db or $student_pet_name_db on that page. I am not getting I used functions to do it than also. The other page I am writing as

students_table("someuserid");
echo $student_pet_name_db;

can any one please tell me know to do this. Thanks.

user2642907
  • 121
  • 1
  • 1
  • 11
  • Check out the answers in the below links http://stackoverflow.com/questions/871858/php-pass-variable-to-next-page http://stackoverflow.com/questions/8127988/passing-variable-between-pages – Kuluval Aug 01 '13 at 16:34

3 Answers3

0

You are not returning anything from your function. Change it so it returns something.

function students_table($students_table_row) {
    $select_db = "select * from students where p_username ='".$students_table_row."'";
    $student_address_query= mysql_query($select_db);
    $students_db = mysql_fetch_assoc($student_address_query);
    return array(
        'id_db' => $students_db['student_id'],
        'pet_name_db' => $students_db['student_pet_name'],
        'name_db' => $students_db['student_name'],
        'p_gender_db' => $students_db['p_gender']
    );
}

Now on your other page, use it like:

$student = students_table("someuserid");
echo $student['pet_name_db'];

Make sure you are requiring the php file where you are using the students_table function, on every file you need the $students var.

chris-l
  • 2,802
  • 1
  • 18
  • 18
0

Try this

function students_table($students_table_row) 
{
    $select_db = "select * from students where p_username ='".$students_table_row."'";
    $student_address_query= mysql_query($select_db);
    $students_db = mysql_fetch_assoc($student_address_query);

    $retArray = array(
        "student_id_db" => $students_db['student_id'],
        "student_pet_name_db" => $students_db['student_pet_name'],
        "student_name_db" => $students_db['student_name'],
        "p_gender_db" => $students_db['p_gender']
    );

    return $retArray;
}

Call it like this

$data = students_table("someuserid");
echo $data["student_pet_name_db"];

Hope this works.

Jay Shukla
  • 5,794
  • 8
  • 33
  • 63
0

What you are looking for are called Session Variables. You can create a session variable on one page, and access its contents on another.

First you must start a session, by including session_start(); at the top of your script. (NOTE: you will need to include this line on each script that you want to access the session variables).

So you will have something like this:

test.php

<?php

session_start();
$_SESSION['test'] = "I'm a session variable";
?>

othertest.php

<?php

session_start();
echo $_SESSION['test'];
?>
Paddyd
  • 1,870
  • 16
  • 26