1

I have 1st PHP page named session1.php with code below:

<?php
session_start();

.....mysql connection...

$sql = "SELECT name1, brand, price  FROM products WHERE name1='cuvette' ORDER BY 'name1' ASC,'desc1' ASC";
$result = mysqli_query($con,$sql);

while($row = mysqli_fetch_array($result)) {
$_SESSION['tag'] = $row;
print_r($_SESSION['tag']);
}

It outputs:

Array ( [0] => Cuvette [name1] => Cuvette [1] => HmbG [brand] => HmbG [2] => 9.00 [price] => 9.00 ) 
Array ( [0] => Cuvette [name1] => Cuvette [1] => HmbG [brand] => HmbG [2] => 8.00 [price] => 8.00 )

Then I setup 2nd PHP page named session2.php with code below:

<?php
session_start();
print_r($_SESSION['tag']);
?>

But session2.php only ouputts:

Array ( [0] => Cuvette [name1] => Cuvette [1] => HmbG [brand] => HmbG [2] => 8.00 [price] => 8.00 ) 

I am expecting the results should be same with session1.php(2 arrays). Anyone has the explanation for this??

2
  • Actually you don't have 2 arrays. You are overwriting the first array with the second. Commented Jun 22, 2017 at 3:01
  • I also want to know: What is the meaning of this[ ] in $_SESSION['tag'][ ] = $row; ? I don't know the meaning that's why not using it. Where can I learn that? Commented Jun 22, 2017 at 4:04

3 Answers 3

1

You call the print_r() in the while loop in the first php file, which may be executed many times, and the session value has been oberrided.

Change your code to this, it will be ok.

$_SESSION['tag'] = [];
while($row = mysqli_fetch_array($result)) {
$_SESSION['tag'][] = $row;

}
print_r($_SESSION['tag']);
Sign up to request clarification or add additional context in comments.

6 Comments

I tried this. I works at first. But if I refresh the page, the array gets double, the refresh again, it gets triple..why??
You can set it to empty array before the while loop.
Now it works. Just a question, dreamweaver mark red on this line red saying syntax errot: $_SESSION['tag'] = []; but is still work. why?
I also want to know: What is the meaning of this[ ] in $_SESSION['tag'][ ] = $row; ? I don't know the meaning that's why not using it. Where can I learn that?
|
1

Your first page is assigning to $_SESSION['tag'] from inside a while loop:

while($row = mysqli_fetch_array($result)) {
  $_SESSION['tag'] = $row;
}

Because you are assigning to a variable, rather than pushing to an array, each time you make the assignment you are overwriting any existing content stored within the session variable.

To resolve this, you can assign an array directly to a $_SESSION variable, such as:

$_SESSION['tag'] = [];
while($row = mysqli_fetch_array($result)) {
  $_SESSION['tag'][] = $row;
}

The output appears to be correct in the first page, because your print statement is within the while loop. The variable is written to, printed out, then overwritten, and printed out again.

Hope this helps! :)

4 Comments

I tried this. I works at first. But if I refresh the page, the array gets double, the refresh again, it gets triple..why??
You need to clear the array beforehand. By setting it as an empty array before adding the result of the query to it, you won't duplicate the data. I've updated my answer to fix this.
I also want to know: What is the meaning of this[ ] in $_SESSION['tag'][ ] = $row; ? I don't know the meaning that's why not using it. Where can I learn that?
The square brackets denote an empty array. It's essentially the same thing as array(), with the only difference being that [] is only supported from PHP 5.4 onwards :)
0

$_SESSION['tag'] is not an array but a variable.You see two arrays because you run print_r twice.

You can compare the following two codes:

while($row = mysqli_fetch_array($result)) {
    $_SESSION['tag'] = $row;   //This is the assignment of variable
}
print_r($_SESSION['tag']);

And the other is:

while($row = mysqli_fetch_array($result)) {
    $_SESSION['tag'][] = $row;  //This is appending the variable to the array
}
print_r($_SESSION['tag']);

Comments

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.