質問

it's hard to explain so I will just say what I have and what I need

I generate 3 arrays with data (a lot of data) from mysql (every time) when user visits any page. I need this for select options in the header of page

overall number of strings ~4000

$a[1]['name']="balallbal";

$a[1]['desc']="balallbal desc";


$a[4000]['name']="balallbal 4000";

$a[4000]['desc']="balallbal desc 4000";

Can I somehow store this data instance or smth so when user goes to another page it would use stored data and not query mysql

役に立ちましたか?

解決

You may want to try memcache. Store your result in memcache when user first request comes in and for next subsequent request you can pull the data from memcache instead of querying sql. Also the time for which data is to be stored in cache is configurable. http://in1.php.net/memcache

他のヒント

Yes you can. you must use $_SESSION for this. $_SESSIONs store data through the pages until the user closes the browser.

do this:

if($_SESSION['data-query'] == '')
{
   $data_array = get_data_from_db();
   $_SESSION['data-query'] = true; // to prevent the next data query
   $_SESSION['cat-data'] = $data_array;
}

Or you can use the count($_SESSION['cat-data']) == 0 instead of 'data-query` (as mentioned in the comment)

Use $_SESSION variable, it keeps your data while user surfs the site.

http://www.php.net/manual/en/features.sessions.php

Check if this variable exists and has your data in the beginning of your code (something like start.php, init.php, header.php. Include it in all your pages). If not - fill it with mysql query one time and than use $_SESSION on other pages.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top