NameSilo

How to pass a long array from one PHP Script to another?

Spaceship Spaceship
Watch

evdoxos

Established Member
Impact
0
How to pass a long array from one PHP Script to another?
My array could have 10000 integers and it is the result from a mysql query.
(I can't execute this query to the target php script, because many other php scripts link to this one passing arrays like this)
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
I know 3 ways:

--------------------------------------------------------------------------

1st way: using $_GET[]
<a href="sample2.php?<*? for ($I=0, $I<count($array); $I++) echo
"array[]={$array[$I]}&"; ?>">
My array has thousand elements, so I can't use this way.

--------------------------------------------------------------------------

2nd way: using $_SESSION[]
// 1st script
<?php
session_start();
// add your array to your session
$_SESSION['my_array'] = array();
?>

// 2nd script
<?php
session_start();
foreach ($_SESSION['my_array']) {
// loop through your array and do your stuff
}
?>
But many users don't accept cookies

--------------------------------------------------------------------------

3rd way:
Using hidden fields and $_POST[]
This way is the only thing I can do, OR is there something more efficient?

--------------------------------------------------------------------------
 
0
•••
Perhaps serialize could be good for this? Not sure how effective it would be on something this long.

http://php.net/serialize

It would be really helpful if you could post more details about the PHP scripts, posting code is always good.

Thanks
 
0
•••
a session would be your best bet. If you have too many and it seems you will have a lot it may end up too much for get or post so you wont be able to pass the information that way.
 
1
•••
Though I am not the master php programmer, it seems like this is the reason to use a database like MySQL in the first place. Do the MySQL query, save the results as an element of a MySQL database, and then pass the MySQL element location from one script to the other.

evdoxos said:
How to pass a long array from one PHP Script to another?
My array could have 10000 integers and it is the result from a mysql query.
(I can't execute this query to the target php script, because many other php scripts link to this one passing arrays like this)
 
0
•••
jagusa said:
Though I am not the master php programmer, it seems like this is the reason to use a database like MySQL in the first place. Do the MySQL query, save the results as an element of a MySQL database, and then pass the MySQL element location from one script to the other.

evdoxos said:
How to pass a long array from one PHP Script to another?
My array could have 10000 integers and it is the result from a mysql query.
(I can't execute this query to the target php script, because many other php scripts link to this one passing arrays like this)

He's passing a database output anyway, so this wouldn't simplify matters.
I don't understand why he can't just run the DB query again on the final PHP page, would seem a lot simpler. :p

Dan
 
0
•••
Daniel said:
He's passing a database output anyway, so this wouldn't simplify matters.
I don't understand why he can't just run the DB query again on the final PHP page, would seem a lot simpler. :p

Dan
Yes it simplifies the matters because: in source scripts he stores IDs only and in the target script he is searching by ID only not by all criteria as he was doing in the source scripts.
And if we think that every source script has different and complicated criteria it makes "run the DB query again" very difficult and could cancel the work of some source scripts (if we do this work again).
 
Last edited:
0
•••
I'm going to agree with Peter on this one, sessions is a much more sensible approach. It is indeed what they are designed for.

And to evdoxos, you state that sessions need cookies. But they don't.

Use the PHP variable SID; to append the session ID data.

Code:
<a href="page.php<?php echo htmlspecialchars(SID); ?>">Page</a>

Most PHP setups should only append the SID if cookies are off, some servers even have it on by default. (session.use_trans_sid setting if I remember correctly.)

Dan

P.S. Why are you referring to yourself in the 3rd person? :P
 
Last edited:
1
•••
Daniel said:
I'm going to agree with Peter on this one, sessions is a much more sensible approach. It is indeed what they are designed for.

And to evdoxos, you state that sessions need cookies. But they don't.

Use the PHP variable SID; to append the session ID data.

Code:
<a href="page.php<?php echo htmlspecialchars(SID); ?>">Page</a>

Most PHP setups should only append the SID if cookies are off, some servers even have it on by default. (session.use_trans_sid setting if I remember correctly.)

Dan

P.S. Why are you referring to yourself in the 3rd person? :P

Thank you!

Code:
file: page1.php
<?php
  session_start();
  $session = session_name() . '=' . session_id();
  // code here
  header("location:page2.php?".$session);
  exit();
?>

file: page2.php
<?php
  session_start();
  // code here
?>
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back