/    Sign up×
Community /Pin to ProfileBookmark

Why $_SESSION Variable Value Auto Changes ?

Php Folks,

When I click the SEARCH button. 5 matching rows found from my mysql db.
Note this:

[code]
$_SESSION[‘row_count’] = 5;
[/code]

Then when I click PAGE 2 on pagination section, it should still be: $_SESSION[‘row_count’] = 5.
But not! It is:

[code]
$_SESSION[‘row_count’] = 0;
[/code]

Why is that ? It should stay ‘5’. Right ? I am not overwriting the variable value either.

This is the reason why, when I click PAGE 2 or PAGE 3 (any pages after PAGE 1) on the PAGINATION section, I see zero results or no rows shown. No rows get shown beyond page 1. Why ?

If I can find-out why the ‘$_SESSION[‘row_count’] = 5′ auto becomes $_SESSION[‘row_count’] = 0, then mystery solved.

Look, after clicking the SEARCH button, this part of my code yields $_SESSION[‘row_count’] = 5. So far so good.

[code]
$query_1 = “SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?”;
$stmt_1 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_1,$query_1))
{
mysqli_stmt_bind_param($stmt_1,”ss”,$_POST[“first_name”],$_POST[“marital_status”]);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
mysqli_stmt_fetch($stmt_1);
$_SESSION[‘row_count’] = $row_count;
[/code]

NOTE:
I get shown the matching rows on PAGE 1. Since I set it to display 1 row per page, I am shown 1 matching row. So far, so good.

Now, when I click PAGE 2 on the PAGINATION section, I expect to see the 2nd matching row, but “$_SESSION[‘row_count’] = 5″ switches to “$_SESSION[‘row_count’] = 0″ and so no matching rows get shown. I repeat: Why the switching of values from ‘5’ to ‘0’ when I click PAGE 2 or onwards ?

This illegal switching ruins this following query that runs when I click PAGE 2 or any PAGE (eg PAGE 3) beyond PAGE 1:

[code]
$row_count = $_SESSION[‘row_count’];
//$total_pages = ceil($result_1/$result_per_page); //Should I keep this line or the line below ? Which one ?
$total_pages = ceil($row_count/$result_per_page); //Should I keep this line or the line above it ? Which one ?
[/code]

I need an answer to my question on the comment above.

Context:

[code]

$query_1 = “SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?”;
$stmt_1 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_1,$query_1))
{
mysqli_stmt_bind_param($stmt_1,”ss”,$_POST[“first_name”],$_POST[“marital_status”]);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
mysqli_stmt_fetch($stmt_1);
$_SESSION[‘row_count’] = $row_count; // $row_count = 5, here.
[/code]

On the above code, check my comment!

[code]
$query_2 = “SELECT id,first_name,middle_name,surname,gender,marital_status,working_status FROM users WHERE first_name = ? AND marital_status = ? ORDER by id LIMIT $offset,$last_row_on_page”;
$stmt_2 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_2,$query_2))
mysqli_stmt_bind_param($stmt_2,”ss”,$_POST[“first_name”],$_POST[“marital_status”]);
mysqli_stmt_execute($stmt_2);
$result_2 = mysqli_stmt_get_result($stmt_2);
//Grab total number of pages to paginate.
$row_count = $_SESSION[‘row_count’]; //Why this value switched from ‘5’ to ‘0’ here ?
//$total_pages = ceil($result_1/$result_per_page);
$total_pages = ceil($row_count/$result_per_page);
[/code]

On the above code, check my comment as I need it’s question answered!

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorJul 01.2020 — Full Code:
<i>
</i>&lt;?php
error_reporting(E_ALL);
?&gt;

&lt;!DOCTYPE HTML"&gt;
&lt;html&gt;

&lt;head&gt;
&lt;meta name="viewport" content="width-device=width, initial-scale=1"&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php
session_start();

if(!isset($_GET['query_type']) &amp;&amp; empty($_GET['query_type']))
{
die("Invalid Query!");
}
else
{
$_SESSION['query_type'] = $_GET['query_type']; echo __LINE__; echo "&lt;br&gt;";//DELETE
}
echo __LINE__; echo "&lt;br&gt;";//DELETE

if(!isset($_GET['form_type']) &amp;&amp; empty($_GET['form_type']))
{
die("Invalid Form!");
}
else
{
$_SESSION['form_type'] = $_GET['form_type']; echo __LINE__; echo "&lt;br&gt;";//DELETE

<i> </i>if(!function_exists($_SESSION['form_type']))
<i> </i>{
<i> </i> die("Invalid Form!");
<i> </i>}
<i> </i>else
<i> </i>{echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i> if(!isset($_SESSION['form_step']))// || $_SESSION['form_step'] != 'end')
<i> </i> {
<i> </i> $_SESSION['form_step'] = 'start'; echo $_SESSION['form_step'];
<i> </i> echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i> $_SESSION['form_type']();
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> $_SESSION['form_step'] = $_GET['form_step'];
<i> </i> echo __LINE__; echo "&lt;br&gt;"; echo $_SESSION['form_step'];//DELETE
<i> </i> $_SESSION['form_type']();
<i> </i> }
<i> </i>}
}

//FUNCTIONS START FROM HERE
function search()
{echo __LINE__; echo "&lt;br&gt;";//DELETE
function rows_count()
{
//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost","root","","powerpage");
$conn-&gt;set_charset('utf8mb4'); //Always set Charset.

<i> </i> if($conn === false)
<i> </i> {
<i> </i> die("ERROR: Connection Error!. " . mysqli_connect_error());
<i> </i> }
<i> </i>
<i> </i> $query_1 = "SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?";
<i> </i> $stmt_1 = mysqli_stmt_init($conn);
<i> </i> if(mysqli_stmt_prepare($stmt_1,$query_1))
<i> </i> {
<i> </i> mysqli_stmt_bind_param($stmt_1,"ss",$_POST["first_name"],$_POST["marital_status"]);
<i> </i> mysqli_stmt_execute($stmt_1);
<i> </i> $result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
<i> </i> mysqli_stmt_fetch($stmt_1);
<i> </i> $_SESSION['row_count'] = $row_count;
<i> </i> echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i> $_SESSION['form_step'] = 'end'; //$form_step = 'end'; WRONG
<i> </i> //fetch_rows();
<i> </i> }
<i> </i> //Close Statement.
<i> </i> mysqli_stmt_close($stmt_1);
<i> </i> //Close Connection.
<i> </i> mysqli_close($conn);
<i> </i>}

<i> </i>function fetch_rows()
<i> </i>{ echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i> $form_step = $_GET['form_step'];
<i> </i>
<i> </i> $page_number = $_GET['page'];
<i> </i> $result_per_page = $_GET['page_limit'];
<i> </i> $offset = (($page_number * $result_per_page) - $result_per_page); //Offset (Row Number that 'Starts' on page).
<i> </i> $last_row_on_page = ($page_number * $result_per_page); //Max Result (Row Number that 'Ends' on page).
<i> </i> $previous_page = $page_number-1;
<i> </i> $next_page = $page_number+1;
<i> </i>
<i> </i> echo "Row Start: $offset";echo "&lt;br&gt;";
<i> </i> echo "Row End: $last_row_on_page";echo "&lt;br&gt;";
<i> </i>
<i> </i> //Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
<i> </i> mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
<i> </i> $conn = mysqli_connect("localhost","root","","powerpage");
<i> </i> $conn-&gt;set_charset('utf8mb4'); //Always set Charset.

<i> </i> if($conn === false)
<i> </i> {
<i> </i> die("ERROR: Connection Error!. " . mysqli_connect_error());
<i> </i> }

<i> </i> $query_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? ORDER BY id LIMIT $offset,$last_row_on_page";
<i> </i> $stmt_2 = mysqli_stmt_init($conn);
<i> </i> if(mysqli_stmt_prepare($stmt_2,$query_2))
<i> </i> {echo __LINE__; echo "&lt;br&gt;";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 111.
<i> </i> mysqli_stmt_bind_param($stmt_2,"ss",$_POST["first_name"],$_POST["marital_status"]);
<i> </i> mysqli_stmt_execute($stmt_2);
<i> </i> $result_2 = mysqli_stmt_get_result($stmt_2);
<i> </i> echo __LINE__; echo "&lt;br&gt;";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 114.
<i> </i> //Grab total number of pages to paginate.
<i> </i> $row_count = $_SESSION['row_count'];
<i> </i> //$total_pages = ceil($result_1/$result_per_page);
<i> </i> $total_pages = ceil($row_count/$result_per_page);
<i> </i>
<i> </i> echo "TOTAL PAGES: $total_pages&lt;br&gt;&lt;br&gt;";
<i> </i>
<i> </i> while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
<i> </i> {echo __LINE__; echo "&lt;br&gt;";
<i> </i> //Retrieve Values.
<i> </i> $id = $row["id"];
<i> </i> $first_name = $row["first_name"];
<i> </i> $middle_name = $row["middle_name"];
<i> </i> $surname = $row["surname"];
<i> </i> $gender = $row["gender"];
<i> </i> $marital_status = $row["marital_status"];
<i> </i> $working_status = $row["working_status"];
<i> </i>
<i> </i> echo "Id: $id&lt;br&gt;";
<i> </i> echo "First Name: $first_name&lt;br&gt;";
<i> </i> echo "Middle Name: $middle_name&lt;br&gt;";
<i> </i> echo "Surname: $surname&lt;br&gt;";
<i> </i> echo "Gender: $gender&lt;br&gt;";
<i> </i> echo "Marital Status: $marital_status&lt;br&gt;";
<i> </i> echo "Working Status: $working_status&lt;br&gt;";
<i> </i> echo "&lt;br&gt;";
<i> </i> echo "&lt;br&gt;";
<i> </i> }
<i> </i> $i = 1;
<i> </i> while($i&lt;=$total_pages)
<i> </i> {
<i> </i> if($i&lt;$total_pages)
<i> </i> {
<i> </i> echo "&lt;a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW_1.php?form_type=";?&gt;&lt;?php echo $_SESSION['form_type'];?&gt;&amp;query_type=&lt;?php echo $_SESSION['query_type'];?&gt;&amp;form_step=end&amp;page_limit=2&amp;page=&lt;?php echo $i;?&gt;'&gt;&lt;?php echo " $i ";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i> elseif($i==$page_number)
<i> </i> {
<i> </i> echo "&lt;a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW_1.php?form_type=";?&gt;&lt;?php echo $_SESSION['form_type'];?&gt;&amp;query_type=&lt;?php echo $_SESSION['query_type'];?&gt;&amp;form_step=end&amp;page_limit=2&amp;page=&lt;?php echo $i;?&gt;'&gt;&lt;?php echo "&lt;b&gt; $i &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i>
<i> </i> $i++;
<i> </i> }
<i> </i> if($page_number&gt;$total_pages)
<i> </i> {
<i> </i> echo "&lt;a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW_1.php?form_type=";?&gt;&lt;?php echo $_SESSION['form_type'];?&gt;&amp;query_type=&lt;?php echo $_SESSION['query_type'];?&gt;&amp;form_step=end&amp;page_limit=2&amp;page=&lt;?php echo $previous_page;?&gt;'&gt;&lt;?php echo "&lt;b&gt; Previous &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i> }
<i> </i> //Close Statement.
<i> </i> mysqli_stmt_close($stmt_2);
<i> </i> //Close Connection.
<i> </i> mysqli_close($conn);
<i> </i>
<i> </i> $_SESSION['form_step'] = 'end';
<i> </i> //die();
<i> </i>}
<i> </i>?&gt;
<i> </i>
<i> </i>&lt;form action="&lt;?php echo $_SERVER['PHP_SELF'];?&gt;?form_type=&lt;?php echo $_SESSION['form_type'];?&gt;&amp;query_type=&lt;?php echo $_SESSION['query_type'];?&gt;&amp;form_step=end&amp;page_limit=2&amp;page=1" method='post' enctype='plain/text'&gt;
<i> </i>&lt;?php

<i> </i>//Added '*' (asterisk) to indicate the 'Text Field' is a 'required' one.
<i> </i>echo "&lt;label for="first_name"&gt;First Name *:&lt;/label&gt;
<i> </i>&lt;input type="text" name="first_name" placeholder="First Name" value = ""&gt;";?&gt;
<i> </i>&lt;br&gt;
<i> </i>&lt;?php
<i> </i>echo "&lt;label for="marital_status"&gt;Marital Status *:&lt;/label&gt;";
<i> </i>echo "&lt;select name="marital_status"&gt;";
<i> </i>echo "&lt;option value="single"&gt;Single&lt;/option&gt;";
<i> </i>echo "&lt;option value="married"&gt;Married&lt;/option&gt;";
<i> </i>echo "&lt;/select&gt;";
<i> </i>echo "&lt;br&gt;";
<i> </i>?&gt;
<i> </i>&lt;input type="submit" name="search" value="Search"&gt;
<i> </i>&lt;?php
<i> </i>//$current_function = __FUNCTION__;
<i> </i>//echo $current_function;
<i> </i>
<i> </i>/*
<i> </i>//Do following if "Search" button clicked.
<i> </i>if($_SERVER['REQUEST_METHOD'] === 'POST')
<i> </i>{echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i> //Do following if "Search" button clicked.
<i> </i> if(isset($_POST['search']))
<i> </i> {echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i> rows_count(); //This function will forward script flow to fetch_rows() before halting the script.
<i> </i> die();
<i> </i> }
<i> </i>}
<i> </i>echo __LINE__; echo "&lt;br&gt;";//On PAGINATION PAGE 2, THIS FAILS TO ECHO. IT IS LINE: 198.
<i> </i>//Do following if "Search" button not clicked but pagination numbered links are clicked. Eg Page 1, 2, 3, etc..
<i> </i>fetch_rows(); //On PAGINATION PAGE 2, THIS FUNCTION IS NOT GETTING TRIGGERED! WHY ? IT IS LINE: 200. MAIN ISSUE HERE, I SUSPECT.
<i> </i>echo __LINE__; echo "&lt;br&gt;";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 201.
<i> </i>die;
<i> </i>*/
<i> </i>
<i> </i>//Do following if "Search" button clicked.
<i> </i>if($_SERVER['REQUEST_METHOD'] === 'POST')
<i> </i>{echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i> //Do following if "Search" button clicked.
<i> </i> if(isset($_POST['search']))
<i> </i> {echo __LINE__; echo "&lt;br&gt;";//DELETE
<i> </i> rows_count(); //This function will forward script flow to fetch_rows() before halting the script.
<i> </i> fetch_rows(); //On PAGINATION PAGE 2, THIS FUNCTION IS NOT GETTING TRIGGERED! WHY ? IT IS LINE: 200. MAIN ISSUE HERE, I SUSPECT.
<i> </i> echo __LINE__; echo "&lt;br&gt;";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 201.
<i> </i> die;
<i> </i> }
<i> </i>}
<i> </i>echo __LINE__; echo "&lt;br&gt;";//On PAGINATION PAGE 2, THIS FAILS TO ECHO. IT IS LINE: 198.
<i> </i>//Do following if "Search" button not clicked but pagination numbered links are clicked. Eg Page 1, 2, 3, etc..
<i> </i>//rows_count(); //This function will forward script flow to fetch_rows() before halting the script.
<i> </i>fetch_rows(); //On PAGINATION PAGE 2, THIS FUNCTION IS NOT GETTING TRIGGERED! WHY ? IT IS LINE: 200. MAIN ISSUE HERE, I SUSPECT.
<i> </i>echo __LINE__; echo "&lt;br&gt;";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 201.
<i> </i>die;
}

?&gt;

Copy linkTweet thisAlerts:
@NogDogJul 01.2020 — Your session_start() is not going to work if you do this, as you are then outputting to the browser before you try to start the session:
<i>
</i>&lt;?php
error_reporting(E_ALL);
?&gt;

&lt;!DOCTYPE HTML"&gt;
&lt;html&gt;

&lt;head&gt;
&lt;meta name="viewport" content="width-device=width, initial-scale=1"&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php
session_start();

Instead, stick the session_start() at the very top (where you have the error_reporting() right now).
Copy linkTweet thisAlerts:
@developer_webauthorJul 03.2020 — @NogDog#1620136

I did that NogDog just now on 6 different php scripts. But no luck. Result of test still same.

You see, when I encountered this problem a week ago, I derived another script from it coding it a little different or shorter and faced the same issue. And so, i derived another and so on. Now, total I have derived 6 versions and all of them encounter same problem. I just added the session start at the top of all 6 files right below error reporting at the top. But no luck. No change in scripts behaviours.

This is a mystery now! For over a week been going round in circles! Drat!
Copy linkTweet thisAlerts:
@developer_webauthorJul 03.2020 — @Sempervivum,

I wonder what you will make out of my latest issue mentioned in my previous post.
Copy linkTweet thisAlerts:
@developer_webauthorJul 03.2020 — @NogDog,

I have created membership (login/register, logout, account homepage, pagination, etc.) pages before with helps of course from people like you and Sempervivum.

The membership script had about 10 different pages.

My new project is that, the membership script should be all one page. So, login, logout, register, search (pagination) all in one page. What I just gave in this thread is the pagination part of the page that i am stuck on. The reg & login parts already work. I did not bother mentioning them parts in this thread to keep the code to the point where I am having issue.

So, there you go. Now you know the full picture.

Anyway, since you suggested to add the session start at the top of the page right under the error reporting code and I did but no luck. I even switched the form method from post to get, like another suggested, but no luck there too.

As for writing errors in a different file or whatever another was advising, I have never done it and so don't know how to proceed, unless ofcourse someone can point me in the right direction with a code snippet or a link to a tutorial.

I have searched all over google for a tutorial with keywords like these but no luck for over a wk now:

pagination tutorial AND mysqli AND prepared statements AND php

Most tutorials are in oop or pdo or mysql extension. Or, they show tutorials on how to build pagination that displays whole table records (all rows). They don't show tutorials (mysqli, prepared statement) how to show records based on keyword search. That is why I am doing things all by myself reading manual and getting ideas from tutorials here and there that are based on oop or pdo or mysql extension or based on "show all table records".

That issue of **$_SESSION['row_count'] = 5** switching to **$_SESSION['row_count'] =** 0 on every page load (same page, remember!), still remains.

(Remember, this membership script is a one page script).

If I can get over this hurdle of why the God Forsaken **$_SESSION['row_count'] = 5** AUTO switches to **$_SESSION['row_count'] = 0** on every same page load, then this 2-3 months project is complete!
Copy linkTweet thisAlerts:
@developer_webauthorJul 07.2020 — Folks,

My form method is not GET but POST.

Concentrate on my code on my op.

Since I got form method to POST, then I have to switch these $_GETs to $_POSTs. Right ?

<i>
</i>
if($_SESSION['form_step'] == 'start');
{
if(!isset($_GET['form_type']) &amp;&amp; empty($_GET['form_type']))
{
die("Invalid Form!");
}
else
{
$_SESSION['form_type'] = $_GET['form_type'];
<br/>
<i> </i> if(!isset($_GET['query_type']) &amp;&amp; empty($_GET['query_type']))
<i> </i> {
<i> </i> die("Invalid Query!");
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> $_SESSION['query_type'] = $_GET['query_type'];
<i> </i>
<i> </i> if(!function_exists($_SESSION['form_type']))
<i> </i> {
<i> </i> die("Invalid Form!");
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> $_SESSION['form_type']();
<i> </i> }
<i> </i> }
<i> </i>}

×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...