2

I am having a bit of an issue passing array variables that are fetched from the database and stored in variables and passing them into session variables to use all over the site.

Code:

$rid=$_SESSION['SESS_MEMBER_ID'] ;

$recquery = 'SELECT * FROM clinic_receptionist,clinic_table where clinic_receptionist.recep_clinic_id = clinic_table.ID AND recep_id ='.$rid;

$recresult = mysqli_query($conn,$recquery);

if(mysqli_num_rows($recresult)>0)
{
while($row = mysqli_fetch_assoc($recresult))
{
$cid = $row['clinic_id'];
$cname = $row['clinic_name'];
$name = $row['recep_full_name'];
$phone = $row['recep_mobile'];
$email = $row['recep_email'];
$address = $row['recep_address'];
$gender = explode(",",$row['recep_gender']);
$dob = $row['recep_dob'];
$doj = $row['recep_doj'];
}
}
?>

The above code is where the select query is happening. I wanted to know how do I assign $cid and $cname to a $_SESSION variable in different pages. Any help would be much appreciated. Thanks

CODE:

<?php session_start();
include('header.php');

include('menu.php');
include('config.php');

$appquery = 'SELECT * FROM appointment_table,clinic_table where clinic_table.ID = appointment_table.clinic_id';
$appresult = mysqli_query($conn,$appquery);
if(mysqli_num_rows($appresult)>0)
{
while($row = mysqli_fetch_array($appresult))
{
$_SESSION['cid'] = $row['ID'];
$_SESSION['cname'] = $row['clinic_name'];
$ptdate = $row['date'];
$pttime = $row['time'];
$ptname = $row['pt_name'];
$ptphone = $row['pt_ph_num'];
$ptemail = $row['pt_email_id'];

}
}
?>

The above code is the place from where I want to receive the id and clinic name.

3
  • $_SESSION['id'] = $cid and $_SESSION['name'] = $cname Commented Dec 11, 2015 at 5:50
  • Is there any specific reason to open & close so many php tags instead of just one? Wouldn't that be neater? Commented Dec 11, 2015 at 6:24
  • @bIgBoY Sorry about that. Commented Dec 11, 2015 at 6:26

2 Answers 2

3
<?php
    // Start the session
    session_start();

    //Assigning the values to $_SESSION
    $_SESSION['cid'] = $row['clinic_id'];

    $_SESSION['cname'] = $row['clinic_name'];;
    ?>

session_start

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry sessions usually confuse me, how do I get the same $cid that will match with $rid
did u mean by $rid corresponding $cid? Since you have given condition in your select query it will result only corresponding $cid.
no $cid corresponds to a clinic id and $rid corresponds to a receptionist id that works in the particular clinic.
I think you should mention the $cid in receptionist table and join with the clinic table.ie each receptionist should have corresponding clinic id right?if so i suppose should do so.
2

You need to call session_start() on each page to initialize the session, of course; after that you can just

$_SESSION['cid'] = $cid;
$_SESSION['cname'] = $cname;

to store those values in $_SESSION.

By the way, someone's going to tell you to use a prepared statement for that query, so it might as well be me.

Perhaps an illustration is in order.

page1.php:

<?php
    session_start();  // find session, or create new one

    include('header.php');
    include('menu.php');
    include('config.php');

    $appquery = 'SELECT * FROM appointment_table,clinic_table where clinic_table.ID = appointment_table.clinic_id';

    $appresult = $conn->query($appquery);
    if(($rowcount = $appresult->num_rows()) > 0)
    {
        // create arrays to pass data
        $cids = array($rowcount);
        $cnames = array($rowcount);
        $i = 0;
        while($row = $appresult->fetch_array())
        {
            // add values to arrays
            $cids[$i] = $row['ID'];
            $cnames[$i] = $row['clinic_name'];
            $i++;
        }
        // add arrays to session
        $_SESSION['cids'] = $cids;
        $_SESSION['cnames'] = $cnames;
    }

page2.php:

<?php
    session_start();  // find session, or create new one

    include('header.php');
    include('menu.php');
    include('config.php');

    // retrieve arrays from session        
    $cnames = $_SESSION['cnames'];
    $cids = $_SESSION['cids'];

    // ... and print them
    $count = count($cids);
    for ($i = 0; $i < $count; $i++)
    {
        echo "cid = " . $cid[$i] . "; cname = " . $cname[$i] . "<br>";
    }

page1.php performs the query and stores cid and cname values in arrays which are then stored in $_SESSION. page2.php retrieves the arrays from $_SESSION and echoes them out. In production, of course, it would be considered more efficient to run the query on the page where it is displayed, and only pass key values between pages.

I've also used object syntax instead of procedural syntax for mysqli, just to show what it looks like. As usual, I leave error handling as an exercise for the reader.

10 Comments

thanks for the reply but will it know from where the session is getting declared?
If there isn't already a session open, php sets a session_id cookie in the browser. The next call to session_start() looks for that cookie in order to resume the session.
session_start() does all the heavy lifting for you; all you need to do is call it at the top of each page.
I have added it in the files but when I echo the variables $clinic_name = $_SESSION['cname'] and $clinic_id = $_SESSION['cid'] it just says Array
Hi, Darwin I have editted the code, and added the code where the cid and cname should be displayed. If you could please have a look.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.